abstract class Animal {
name: String
sex: string
constructor(name: string, sex: string) {
this.name = name
this.sex = sex
}
// sayHello() {
// console.log('hello')
// }
/**
* 抽象类
* 以abstract开头的类,抽象类和其他的类区别不大,只是不能用来创建对象。
* 抽象类就是专门用来被继承的类,抽象类中可以添加抽象方法
*/
abstract sayHello(): void;
}
//继承
class Dog extends Animal {
age: number
constructor(name: string, sex: string, age: number) {
//super超类
super(name, sex)
this.age = age
}
// 重写
sayHello() {
console.log('汪汪汪')
}
}
const dog = new Dog('小黑', '男', 2)
console.log(dog.sayHello)
TypeScript——继承、super、抽象类
最新推荐文章于 2024-09-12 19:39:54 发布