【问题标题】:Inheritance in TypeScriptTypeScript 中的继承
【发布时间】:2016-11-18 05:53:55
【问题描述】:

Typescript 中是否有可能的继承特性以及重载、覆盖等其他特性?

【问题讨论】:

    标签: typescript typescript1.8 typescript1.5 typescript1.6 typescript1.4


    【解决方案1】:
    class Animal {
        name: string;
        constructor(theName: string) { this.name = theName; }
        move(distanceInMeters: number = 0) {
            console.log(`${this.name} moved ${distanceInMeters}m.`);
        }
    }
    
    class Snake extends Animal {
        constructor(name: string) { super(name); }
        move(distanceInMeters = 5) {
            console.log("Slithering...");
            super.move(distanceInMeters);
        }
    }
    
    class Horse extends Animal {
        constructor(name: string) { super(name); }
        move(distanceInMeters = 45) {
            console.log("Galloping...");
            super.move(distanceInMeters);
        }
    }
    
    let sam = new Snake("Sammy the Python");
    let tom: Animal = new Horse("Tommy the Palomino");
    
    sam.move();
    tom.move(34);
    

    【讨论】:

    • lage raho bhaiyon :D
    猜你喜欢
    • 2016-04-03
    • 2021-08-17
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2020-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多