在类的方法中,super表示当前类的父类

    如果子类中写了构造函数,在子类的构造函数中必须对父类的构造函数进行调用
    super();
    记着还要将父类的参数再调一遍

    1. class Animal{
    2. name:string;
    3. constructor(name:string){
    4. // 在实例方法中,this就表示当前的实例
    5. // 在构造函数中当前对象就是当前新建的那个对象
    6. // 可以通过this向新建的对象中添加属性
    7. this.name = name;
    8. }
    9. sayHello(){
    10. // 在方法中可以通过this来表示当前调用方法的对象
    11. console.log('rrrr');
    12. }
    13. }
    14. // 使Dog类继承Animal类
    15. // Animal被称为父类,Dog被称为子类
    16. class Dog extends Animal{
    17. age: number;
    18. constructor(name:string,age:number){
    19. super(name);//调用父类的构造函数
    20. this.age = age;
    21. }
    22. sayHello(){
    23. super.sayHello();
    24. }
    25. }
    26. const dog = new Dog('name1',18);
    27. dog.sayHello();