当前位置:网站首页>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 CD81 monoclonal antibody related parameters and Applications
- 启牛2980有没有用?开户安全吗、
- Clion-MinGW编译后的exe文件添加ico图标
- Viewrootimpl and windowmanagerservice notes
- 中国管理科学研究院凝聚行业专家,傅强荣获智库专家“十佳青年”称号
- leetcode:1755. 最接近目标值的子序列和
- 渗透创客精神文化转化的创客教育
- Monorepo管理方法论和依赖安全
- Influence of oscilloscope probe on signal source impedance
猜你喜欢
Open source SPL eliminates tens of thousands of database intermediate tables
Graph embedding learning notes
Influence of oscilloscope probe on measurement bandwidth
Typhoon is coming! How to prevent typhoons on construction sites!
线程池的使用
解析创客教育的知识迁移和分享精神
基于flask写一个接口
Duchefa丨MS培养基含维生素说明书
Clion-MinGW编译后的exe文件添加ico图标
Abnova maxpab mouse derived polyclonal antibody solution
随机推荐
国外LEAD美国简称对照表
Cutting edge technology for cultivating robot education creativity
PVC 塑料片BS 476-6 火焰传播性能测定
systemd-resolved 开启 debug 日志
phpstudy小皮的mysql点击启动后迅速闪退,已解决
haas506 2.0开发教程 - 阿里云ota - pac 固件升级(仅支持2.2以上版本)
Abnova e (diii) (WNV) recombinant protein Chinese and English instructions
Phpstudy Xiaopi's MySQL Click to start and quickly flash back. It has been solved
ODPs next map / reduce preparation
字典树简单入门题(居然是蓝题?)
Simple getting started example of Web Service
Duchefa丨S0188盐酸大观霉素五水合物中英文说明书
模式-“里氏替换原则”
Is it safe to open a stock account by mobile phone? My home is relatively remote. Is there a better way to open an account?
Careercup its 1.8 serial shift includes problems
教你自己训练的pytorch模型转caffe(一)
How to renew NPDP? Here comes the operation guide!
挖财商学院给的证券账户安全吗?可以开户吗?
使用WebAssembly在浏览器端操作Excel
Duchefa low melting point agarose PPC Chinese and English instructions