Xem ví dụ kế thừa từ sân chơi trên trang TypeScript:
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);
Tôi đã thay đổi một dòng mã: cảnh báo trong Horse.move(). Ở đó tôi muốn truy cập super.name, nhưng nó chỉ trả về undefined. IntelliSense gợi ý rằng tôi có thể sử dụng nó và TypeScript biên dịch tốt, nhưng nó không hoạt động.
Có ý kiến gì không?