ts14_super关键字

(function () {
    class Animal {
        name: string
        constructor(name: string) {
            this.name = name
        }
        sayhello() {
            console.log("动物在叫");

        }
    }
    class Dog extends Animal {
        age:number
        constructor(name:string,age:number){
            //如果在子类中写了构造函数,因为在子类中添加了和父类相同的方法则子类方法会覆盖掉父类的方法
            //为了不覆盖父类的构造方法所以要在子类的构造函数中调用父类的构造函数:super(传入父类构造函数中的所需要的属性)
            super(name)
            this.age = age;
        }
        sayhello(): void {
            //在类的方法中super表示当前类的父类
            super.sayhello()
            console.log("汪汪汪");

        }
    }
    const dog1 = new Dog("旺财",2)
    dog1.sayhello()
})()

 

posted @ 2022-12-26 15:29  SadicZhou  阅读(18)  评论(0编辑  收藏  举报