当前位置:网站首页>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
边栏推荐
- 欢迎来战,赢取丰厚奖金:Code Golf 代码高尔夫挑战赛正式启动
- Applet global configuration
- 王老吉药业“关爱烈日下最可爱的人”公益活动在南京启动
- Which is the best online collaboration product? Microsoft loop, notion, flowus
- 2.8 basic knowledge of project management process
- Model method
- Where is a good stock account? Is online account manager safe to open an account
- Norgen AAV extractant box instructions (including features)
- Simple understanding of interpolation search
- Abbkine trakine F-actin Staining Kit (green fluorescence) scheme
猜你喜欢
Wanglaoji pharmaceutical's public welfare activity of "caring for the most lovely people under the scorching sun" was launched in Nanjing
如何让化工企业的ERP库存账目更准确
Duchefa s0188 Chinese and English instructions of spectinomycin hydrochloride pentahydrate
鸿蒙os第四次学习
ProSci LAG3抗体的化学性质和应用说明
Abnova丨培养细胞总 RNA 纯化试剂盒中英文说明书
Typhoon is coming! How to prevent typhoons on construction sites!
解析创客教育的知识迁移和分享精神
Abnova DNA marker high quality control test program
研学旅游实践教育的开展助力文旅产业发展
随机推荐
重上吹麻滩——段芝堂创始人翟立冬游记
Abnova丨 CD81单克隆抗体相关参数和应用
Abnova CRISPR spcas9 polyclonal antibody protocol
haas506 2.0开发教程 - 阿里云ota - pac 固件升级(仅支持2.2以上版本)
研学旅游实践教育的开展助力文旅产业发展
Applet page navigation
解析五育融合之下的steam教育模式
Classic implementation of the basic method of intelligent home of Internet of things
Duchefa cytokinin dihydrozeatin (DHZ) instructions
E. Singhal and numbers (prime factor decomposition)
Abnova cyclosporin a monoclonal antibody and its research tools
14、Transformer--VIT TNT BETR
Abnova CD81 monoclonal antibody related parameters and Applications
Selenium element information
Abnova e (diii) (WNV) recombinant protein Chinese and English instructions
AI automatically generates annotation documents from code
Cutting edge technology for cultivating robot education creativity
Model method
Abnova丨DNA 标记高质量控制测试方案
基于AVFoundation实现视频录制的两种方式