ts get和set

阎小妍

关注

阅读 262

2022-08-27

 

 

 

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 直接对象属性赋值

 



精彩评论(0)

0 0 举报