相关疑难解决方法(0)

在TypeScript中声明抽象方法

我试图弄清楚如何在TypeScript中正确定义抽象方法:

使用原始继承示例:

class Animal {
    constructor(public name) { }
    makeSound(input : string) : string;
    move(meters) {
        alert(this.name + " moved " + meters + "m.");
    }
}

class Snake extends Animal {
    constructor(name) { super(name); }
    makeSound(input : string) : string {
        return "sssss"+input;
    }
    move() {
        alert("Slithering...");
        super.move(5);
    }
}
Run Code Online (Sandbox Code Playgroud)

我想知道如何正确定义方法makeSound,因此它是键入的并且可能被覆盖.

此外,我不知道如何正确定义protected方法 - 它似乎是一个关键字,但没有效果,代码将无法编译.

typescript

171
推荐指数
2
解决办法
11万
查看次数

标签 统计

typescript ×1