TS中的抽象类 多态

//多态:父类定义一个方法不去实现,让继承他的子类去实现,每个子类有不同的表现

 //抽象类: 是提供其他类继承的基类,不能直接被实例化

//用abstract 关键字定义抽象类和抽象方法,抽象类中的抽象方法不包含具体实现并且必须在派生类中实现

//抽象方法只能 放在抽象类里

//抽象类和抽象方法用来定义标准,标准:Animal 这个类要求他的子类必须包含eat方法
 

//abstract  class Animal{

       //abstract eat():any(){}

//}

class Animal{

        public name:string;

        constructor(name:string){

                this.name = name

        }

        abstract eat():any;

}

var a=new Animal()//错误写法



clas Dog extends Animal{

        //抽象类的子类必须实现抽象类里面的方法

        constructor(name:string){

                super(name)

        }

        eat(){

                console.log(this.name+'111')

        }

}

var d = new Dog('小明')
d.eat();

猜你喜欢

转载自blog.csdn.net/Frazier1995/article/details/120605175