当前位置:网站首页>ts 之 属性的修饰符public、private、protect
ts 之 属性的修饰符public、private、protect
2022-07-05 20:44:00 【qq_46302247】
public修饰的属性可以在任意位置访问和修改
class Animal{
name: string //等同于 public name: string; public是默认修饰符
age: number
constructor(name: string, age: number) {
this.name = name
this.age = age
}
}
也可以换成如下写法:
class Animal{
constructor(public name: string, public age: number) {
}
}
const a = new Animal('tt', 19)
console.log(a);//Animal {name: 'tt', age: 19}
private修饰的属性是私有属性,私有属性只能在类的内部访问和修改,它的子类也无法访问和修改;它的实例也无法访问
class Animal{
private name: string
private age: number
constructor(name: string, age: number) {
this.name = name
this.age = age
}
}
如果想访问private修饰的属性,可以通过 set/get
class Animal{
private _name: string
private _age: number
constructor(name: string, age: number) {
this._name = name
this._age = age
}
get age() {
return this._age
}
set age(value) {
if(value>=0) {
this._age = value
}
}
}
const a = new Animal('aa', 15)
console.log(a.age);//15
a.age = -33
console.log(a.age);//15
a.age = 19
console.log(a.age);//19
protect受保护的属性,只能在当前类,和它的子类中访问和修改,但是他们的实例都不能访问
class Animal{
protected _name: string
protected _age: number
constructor(name: string, age: number) {
this._name = name
this._age = age
}
}
class Dog extends Animal{
test() {
console.log(this._age);
}
}
const dog = new Dog('小黑', 18)
dog.test()//18
边栏推荐
- matplotlib绘图润色(如何形成高质量的图,例如设如何置字体等)
- Wanglaoji pharmaceutical's public welfare activity of "caring for the most lovely people under the scorching sun" was launched in Nanjing
- 小程序项目结构
- Propping of resources
- 重上吹麻滩——段芝堂创始人翟立冬游记
- Abbkine trakine F-actin Staining Kit (green fluorescence) scheme
- 资源道具化
- Where is a good stock account? Is online account manager safe to open an account
- Applet event binding
- 小程序全局配置
猜你喜欢
Abnova DNA marker high quality control test program
Duchefa丨S0188盐酸大观霉素五水合物中英文说明书
14、Transformer--VIT TNT BETR
王老吉药业“关爱烈日下最可爱的人”公益活动在南京启动
Analyze the knowledge transfer and sharing spirit of maker Education
教你自己训练的pytorch模型转caffe(一)
AI 从代码中自动生成注释文档
Duchefa s0188 Chinese and English instructions of spectinomycin hydrochloride pentahydrate
Typhoon is coming! How to prevent typhoons on construction sites!
How to make ERP inventory accounts of chemical enterprises more accurate
随机推荐
How to form standard interface documents
解析创客教育的知识迁移和分享精神
Relationship between mongodb documents
Which securities is better for securities account opening? Is online account opening safe?
研学旅游实践教育的开展助力文旅产业发展
教你自己训练的pytorch模型转caffe(一)
2.8 basic knowledge of project management process
Duchefa丨S0188盐酸大观霉素五水合物中英文说明书
【UE4】UnrealInsight获取真机性能测试报告
Abnova丨 CD81单克隆抗体相关参数和应用
小程序全局配置
中国管理科学研究院凝聚行业专家,傅强荣获智库专家“十佳青年”称号
教你自己训练的pytorch模型转caffe(二)
Leetcode (347) - top k high frequency elements
Abnova CRISPR spcas9 polyclonal antibody protocol
Informatics Olympiad 1338: [example 3-3] hospital setting | Luogu p1364 hospital setting
Popular science | does poor English affect the NPDP exam?
Duchefa细胞分裂素丨二氢玉米素 (DHZ)说明书
Interpreting the daily application functions of cooperative robots
Material Design组件 - 使用BottomSheet展现扩展内容(二)