TypeScript中abstract抽象类和抽象方法

2020-10-212200次阅读TypeScript

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

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

abstract修饰的方法(抽象方法)只能放在抽象类里面

抽象类和抽象方法用来定义标准(比如定义标准为:抽象类Animal有抽象方法eat,要求它的子类必须包含eat方法)

abstract class People {
    name : string
    constructor (name:string) {
        this.name = name
    }
    abstract eat (food:string) :void;//抽象方法不包括具体实现,并且必须再派生类中实现
}

class Stud1 extends People {
    //抽象类的子类必须实现抽象类中的抽象方法
    constructor (name:string) {
        super(name)
    }
    eat(food: string): void {
        console.log(`我爱吃 ${food}`)
    }

}

const stu11 : Stud1 = new Stud1('liu')
stu11.eat('面条')

 

上一篇: TypeScript中class类与interface接口  下一篇: clip-path轻松实现三角形多边形  

TypeScript中abstract抽象类和抽象方法相关文章