当前位置:网站首页>Modifiers of attributes of TS public, private, protect
Modifiers of attributes of TS public, private, protect
2022-07-05 20:57:00 【qq_ forty-six million three hundred and two thousand two hundre】
public Decorated attributes can be accessed and modified anywhere
class Animal{
name: string // Equate to public name: string; public It's the default modifier
age: number
constructor(name: string, age: number) {
this.name = name
this.age = age
}
}
It can also be written as follows :
class Animal{
constructor(public name: string, public age: number) {
}
}
const a = new Animal('tt', 19)
console.log(a);//Animal {name: 'tt', age: 19}
private The decorated attribute is a private attribute , Private attributes can only be accessed and modified inside the class , Its subclasses cannot be accessed and modified ; Its instances are also inaccessible
class Animal{
private name: string
private age: number
constructor(name: string, age: number) {
this.name = name
this.age = age
}
}
If you want to visit private Decorated attributes , Can pass 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 Protected properties , Only in the current class , And its subclasses , But none of their instances can be accessed
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(' Little black ', 18)
dog.test()//18
边栏推荐
- Abnova fluorescent dye 620-m streptavidin scheme
- 教你自己训练的pytorch模型转caffe(三)
- haas506 2.0开发教程 - 阿里云ota - pac 固件升级(仅支持2.2以上版本)
- matplotlib绘图润色(如何形成高质量的图,例如设如何置字体等)
- poj 3414 Pots (bfs+线索)
- Duchefa low melting point agarose PPC Chinese and English instructions
- Abnova DNA marker high quality control test program
- Talk about my fate with some programming languages
- AITM2-0002 12s或60s垂直燃烧试验
- php中explode函数存在的陷阱
猜你喜欢

2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc

中国的软件公司为什么做不出产品?00后抛弃互联网;B站开源的高性能API网关组件|码农周刊VIP会员专属邮件周报 Vol.097

leetcode:1755. 最接近目标值的子序列和

Abnova maxpab mouse derived polyclonal antibody solution

The development of research tourism practical education helps the development of cultural tourism industry

Duchefa low melting point agarose PPC Chinese and English instructions
![最长摆动序列[贪心练习]](/img/e1/70dc21b924232c7e5e3da023a4bed2.png)
最长摆动序列[贪心练习]

五层网络协议

Duchefa d5124 md5a medium Chinese and English instructions

ProSci LAG3抗体的化学性质和应用说明
随机推荐
木板ISO 5660-1 热量释放速率摸底测试
Matplotlib drawing retouching (how to form high-quality drawings, such as how to set fonts, etc.)
MYSQL IFNULL使用功能
中国的软件公司为什么做不出产品?00后抛弃互联网;B站开源的高性能API网关组件|码农周刊VIP会员专属邮件周报 Vol.097
Graph embedding learning notes
Talk about my fate with some programming languages
国外LEAD美国简称对照表
CLion配置visual studio(msvc)和JOM多核编译
MySQL InnoDB架构原理
获取前一天的js(时间戳转换)
序列联配Sequence Alignment
Abnova CD81 monoclonal antibody related parameters and Applications
显示屏DIN 4102-1 Class B1防火测试要求
ProSci LAG3抗体的化学性质和应用说明
Hdu2377bus pass (build more complex diagram +spfa)
从架构上详解技术(SLB,Redis,Mysql,Kafka,Clickhouse)的各类热点问题
Traps in the explode function in PHP
教你自己训练的pytorch模型转caffe(一)
WPF gets the control in the datagridtemplatecolumn of the specified row and column in the DataGrid
Duchefa cytokinin dihydrozeatin (DHZ) instructions