属性的封装
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);