class Animal { private _name: string; private _age: number; constructor(name: string,age: number){ this._name = name; this._age = age; } sayHello(){ console.log("hello"); } get name(){ return this._name; } set name(name){ this._name = name; } get age(){ return this._age; } set age(age){ this._age = age; } } const an = new Animal("小黑",3); an.name = "小红" console.log(an);
标签:set,封装,name,age,number,private,._,属性 From: https://www.cnblogs.com/ixtao/p/17557061.html