请参阅 TypeScript 网站上 Playground 的继承示例:
class Animal {
public name;
constructor(name) {
this.name = name;
}
move(meters) {
alert(this.name + " moved " + meters + "m.");
}
}
class Snake extends Animal {
constructor(name) {
super(name);
}
move() {
alert("Slithering...");
super.move(5);
}
}
class Horse extends Animal {
constructor(name) {
super(name);
}
move() {
alert(super.name + " is Galloping...");
super.move(45);
}
}
var sam = new Snake("Sammy the Python");
var tom: Animal = new Horse("Tommy the Palomino");
sam.move();
tom.move(34);
我更改了一行代码:Horse.move()
. 那里我想访问super.name
,但返回只是undefined
。IntelliSense 建议我可以使用它并且 TypeScript 可以正常编译,但它不起作用。
有任何想法吗?