【发布时间】:2015-09-14 16:19:27
【问题描述】:
阅读来自typescript manual的例子:
class Animal {
name:string;
constructor(theName: string) { this.name = theName; }
move(meters: number = 0) {
alert(this.name + " moved " + meters + "m.");
}
}
class Snake extends Animal {
constructor(name: string) { super(name); }
move(meters = 5) {
alert("Slithering...");
super.move(meters);
}
}
class Horse extends Animal {
constructor(name: string) { super(name); }
move(meters = 45) {
alert("Galloping...");
super.move(meters);
}
}
var sam = new Snake("Sammy the Python");
var tom: Animal = new Horse("Tommy the Palomino");
sam.move();
tom.move(34);
问题是关于var tom: Animal = new Horse("Tommy the Palomino");这一行:
据我了解,
tom是一个具有Horse属性的Animal。那正确吗?这样做有什么意义?不声明为
var tom: Horse = ...?只有一个版本让他有机会降级/更改/进化为
Snake或任何其他Animal。我说的对吗?...或者这只是一个错字?
【问题讨论】:
标签: class oop object inheritance typescript1.4