private name: string; // 这里把 name 修饰符改为 private..............................................11
constructor(name: string) {..............................................................................................11
this.name = name;............................................................................................................11
}........................................................................................................................................11
sayHello(): void {.............................................................................................................11
alert("hello animal:" + this.name);...................................................................................11
}........................................................................................................................................11
}.........................................................................................................................................11
class Cat extends Animal {...............................................................................................11
sayHello(): void {.............................................................................................................11
alert("hello cat:" + this.name); //这里会报错,因为无法引用父类 private 修饰的属性
...........................................................................................................................................11
}........................................................................................................................................11
}.........................................................................................................................................11
class Dog extends Animal {..............................................................................................11
sayHello(): void {.............................................................................................................11
alert("hello dog:" + this.name); //这里会报错,因为无法引用父类 private 修饰的属
性.......................................................................................................................................12
}........................................................................................................................................12
}10.4 Get/Set 访问器........................................................................................................12
class Animal {...................................................................................................................12
private name: string;.........................................................................................................12
get name(): string { //通过 get 和 set 解决子类不能引用父类 private 修饰的属性的问
题.......................................................................................................................................12
return this.name;...............................................................................................................12
}........................................................................................................................................12
set name(name: string) {..................................................................................................12
this.name = name;............................................................................................................12
}........................................................................................................................................12
constructor(name: string) {..............................................................................................12
this.name = name;............................................................................................................12
}........................................................................................................................................12
sayHello(): void {.............................................................................................................12
alert("hello animal:" + this.name);...................................................................................12
}........................................................................................................................................12
}.........................................................................................................................................12
class Cat extends Animal {...............................................................................................12
sayHello(): void {.............................................................................................................12
alert("hello cat:" + this.name);.........................................................................................12
}........................................................................................................................................12
}.........................................................................................................................................12
class Dog extends Animal {..............................................................................................12
sayHello(): void {.............................................................................................................12
alert("hello dog:" + this.name);........................................................................................12