kingBook

导航

TypeScript 里的 override

export class Animal {
    
    public say():void{
        console.log("I am a Animal");
    }
}

export class Dog extends Animal {
    
    public override say():void{
        console.log("I am a Dog");
    }
}
let animal: Animal = new Dog();
animal.say(); // output: I am a Dog

let dog: Dog = new Dog();
dog.say();// output: I am a Dog
  • 当子类方法的名称、参数、返回值都相同时,则视为覆盖基类的方法,override 关键字可以省略

posted on 2024-10-26 17:07  kingBook  阅读(2)  评论(0编辑  收藏  举报