class Animal{
name: string
age: number
constructor(name:string,age:number) {
this.name = name
this.age=age
}
sayHello() {
console.log('hello,',this.name,this.age)
}
}
class Cat extends Animal { //子类继承
}
let cat1 = new Cat('小猫',3) //实例化子类
cat1.sayHello() //
//调用继承来的方法 'hello,''小猫',3
class Dog extends Animal {//子类继承
}
let dog1=new Dog('小狗',5) //实例化子类
dog1.sayHello() //'hello,''小狗',5
//执行继承来的方法
console.log(dog1.name) //小狗
//使用继承来的属性
使用继承的方法是,定义一个公共父类,子类通过extends 来定义,子类定义继承父类的时候,实例化子类后,会自动继承父类的方法和属性