ts get和set

 

 

 

class User {
    //  get、set方法的成员变量命名时建议在前面加 _
  private _fullName: string;
    //get 的用法
    get fullName(): string{ 
        return this._fullName;
    }

    // "set" 访问器必须正好具有一个参数
    set fullName(fullName: string) {
        this._fullName = fullName;
    }
}

const c = new User();
// set 不需要调用方法 直接对象属性赋值
c.fullName = "GT";
// get 直接对象属性赋值
console.log(c.fullName)

 

posted @ 2022-08-24 21:01  明媚下雨天  阅读(734)  评论(0编辑  收藏  举报