/*
多态
ts中实现多态的三要素
1、继承
2、重写
3、父类的引用指向之类对象(不是必须?)
class Animal{
name:string;
constructor(name:string){
this.name = name;
}
eat(){
}
}
class Dog extends Animal{
constructor(name:string){
super(name);
}
eat(){
console.log(this.name + '爱吃骨头');
}
}
class Cat extends Animal{
constructor(name:string){
super(name);
}
eat(){
console.log(this.name + '爱吃鱼');
}
}
let dog:Animal = new Dog('小狗');
dog.eat();
let cat:Animal = new Cat('小猫');
cat.eat();
---------------------------------------------------------
抽象类
使用关键字abstract来修饰类。使用抽象类需要注意以下几点 :
1、抽象类不能被实例化
1、抽象类中的抽象方法,不能有具体实现。具体实现必须由子类来完成
3、抽象方法只能放在抽象类中
4、抽象类的具体左右实际上是来定义标准。就是继承了抽象类的子类,必须实现抽象类的抽象方法
例子 :
abstract class Animal{
name:string;
constructor(name:string){
this.name = name;
}
abstract eat():any;
}
class Dog extends Animal{
constructor(name:string){
super(name)
}
eat(){
console.log(this.name + '爱吃骨头');
}
}
class Cat extends Animal{
constructor(name:string){
super(name)
}
eat(){
console.log(this.name + '爱吃鱼');
}
}
let dog:Animal = new Dog('小狗');
dog.eat();
let cat:Animal = new Cat('小猫');
cat.eat();
* */