class Person { // 姓氏 firstName: string // 名字 lastName: string // 信息 private _age: number = 20 // 构造方法 constructor(firstName: string, lastName: string) { this.firstName = firstName this.lastName = lastName } // 读取(get) get fullName () { return this.firstName + '-' + this.lastName } // 设置(set) set fullName (val) { let names = val.split('-') this.firstName = names[0] this.lastName = names[1] } // 获取年龄 get age (): number { return this._age } // 设置年龄,支持修饰符,不写默认就是 public public set age (val) { this._age = val } }