请参见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()
中的alert .我想访问super.name
,但返回的是undefined
.IntelliSense建议我可以使用它,TypeScript编译得很好,但它不起作用.
有什么 idea 吗?