我可以添加些什么以使年龄公开?我必须添加一些内容而不要更改它。
班级人物{
public name: string;
private age: number;
constructor(name: string; age:number) {
this.name=name;
this.age=age; }
}
答案 0 :(得分:0)
您需要一个Getter和Setter:
class Person {
public name: string;
private age: number;
constructor(name: string; age:number) {
this.name=name;
this.age=age;
}
get age():number {
return this.age;
}
set age(age:number) {
this.age = age;
}
}