获取并设置打字稿?

时间:2018-11-08 13:33:52

标签: typescript types public object-oriented-analysis

我可以添加些什么以使年龄公开?我必须添加一些内容而不要更改它。

班级人物{

   public name: string;
   private age: number; 



 constructor(name: string;  age:number) {

            this.name=name;
            this.age=age;   }

}

1 个答案:

答案 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;
    }
}