访问器

# 访问器

作用:控制属性的读取和赋值

class Test {
    constructor(public name: string, private _age: number) {

    }
    get age() {
        return this._age;
    }
    set age(value) {
        this._age = value;
    }
    sayAge() {
        return this._age;
    }
}


let t = new Test('Sam', 18)
t.age = 20;
console.log(t.sayAge(),t.age);

同ES6语法相同。