(function () { // 定义一个animal类 class Animal{ name: string; age: number; constructor(name: string, age: number) { this.name = name; this.age = age; } sayHello() { console.log("动物在叫"); } } /* 此时Animal被称为父类,Dog被称为子类 使用继承后,子类将会拥有父类所有的方法和属性 通过继承可以将多个类中共有的代码写在一个父类中, 如果希望在子类中添加一些父类中没有的属性或方法直接加就行了 如果在子类中添加了和父类相同的方法,则子类方法会覆盖掉父类方法 这种子类覆盖掉父类方法的形式,我们称为方法的重写 父类的静态方法及属性会被子类继承,但不能被子类重写 */ // 定义一个表示狗的类 class Dog extends Animal{ run() { console.log(`${this.name}在跑。。。`); } sayHello() { console.log("汪汪汪"); } } // 定义一个猫的类 class Cat extends Animal { sayHello() { console.log("喵喵喵"); } } const dog = new Dog("旺财", 5); const cat = new Cat("旺财", 5);})()