当前位置:网站首页>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
边栏推荐
- Duchefa丨S0188盐酸大观霉素五水合物中英文说明书
- MySQL ifnull usage function
- How to renew NPDP? Here comes the operation guide!
- 字典树简单入门题(居然是蓝题?)
- Phpstudy Xiaopi's MySQL Click to start and quickly flash back. It has been solved
- 教你自己训练的pytorch模型转caffe(三)
- Analyze the knowledge transfer and sharing spirit of maker Education
- Web Service简单入门示例
- Monorepo management methodology and dependency security
- Abnova DNA marker high quality control test program
猜你喜欢
PVC 塑料片BS 476-6 火焰传播性能测定
珍爱网微服务底层框架演进从开源组件封装到自研
Abnova丨血液总核酸纯化试剂盒预装相关说明书
Duchefa cytokinin dihydrozeatin (DHZ) instructions
Abnova e (diii) (WNV) recombinant protein Chinese and English instructions
Duchefa丨S0188盐酸大观霉素五水合物中英文说明书
五层网络协议
教你自己训练的pytorch模型转caffe(三)
Duchefa丨MS培养基含维生素说明书
显示屏DIN 4102-1 Class B1防火测试要求
随机推荐
Duchefa s0188 Chinese and English instructions of spectinomycin hydrochloride pentahydrate
国外LEAD美国简称对照表
php中explode函数存在的陷阱
木板ISO 5660-1 热量释放速率摸底测试
Who the final say whether the product is good or not? Sonar puts forward performance indicators for analysis to help you easily judge product performance and performance
SQL series (basic) - Chapter 2 limiting and sorting data
How to open an account online for futures? Is it safe?
systemd-resolved 开启 debug 日志
MySQL InnoDB架构原理
Duchefa cytokinin dihydrozeatin (DHZ) instructions
When a user logs in, there is often a real-time drop-down box. For example, entering an email will @qq com,@163. com,@sohu. com
Duchefa MS medium contains vitamin instructions
Clear app data and get Icon
Abnova CD81 monoclonal antibody related parameters and Applications
概率论机器学习的先验知识(上)
Simple getting started example of Web Service
MySQL fully parses json/ arrays
Duchefa丨P1001植物琼脂中英文说明书
ODPs next map / reduce preparation
MySQL ifnull usage function