当前位置:网站首页>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
边栏推荐
- 研學旅遊實踐教育的開展助力文旅產業發展
- Typhoon is coming! How to prevent typhoons on construction sites!
- Open source SPL eliminates tens of thousands of database intermediate tables
- 清除app data以及获取图标
- sql系列(基础)-第二章 限制和排序数据
- Duchefa p1001 plant agar Chinese and English instructions
- matplotlib绘图润色(如何形成高质量的图,例如设如何置字体等)
- Specification of protein quantitative kit for abbkine BCA method
- Duchefa d5124 md5a medium Chinese and English instructions
- Abnova CD81 monoclonal antibody related parameters and Applications
猜你喜欢
Influence of oscilloscope probe on signal source impedance
解析五育融合之下的steam教育模式
【案例】元素的显示与隐藏的运用--元素遮罩
MySQL fully parses json/ arrays
台风来袭!建筑工地该如何防范台风!
Norgen AAV提取剂盒说明书(含特色)
Analyze the knowledge transfer and sharing spirit of maker Education
Duchefa丨D5124 MD5A 培养基中英文说明书
显示屏DIN 4102-1 Class B1防火测试要求
Écrire une interface basée sur flask
随机推荐
Return to blowing marshland -- travel notes of zhailidong, founder of duanzhitang
ts 之 属性的修饰符public、private、protect
The development of research tourism practical education helps the development of cultural tourism industry
Implementation of redis unique ID generator
启牛2980有没有用?开户安全吗、
树莓派4B上ncnn转换出来的模型调用时总是崩溃(Segment Fault)的原因
Duchefa p1001 plant agar Chinese and English instructions
ODPS 下一个map / reduce 准备
leetcode:1755. 最接近目标值的子序列和
hdu2377Bus Pass(构建更复杂的图+spfa)
示波器探头对测量带宽的影响
Écrire une interface basée sur flask
王老吉药业“关爱烈日下最可爱的人”公益活动在南京启动
bazel是否有学习的必要
CADD course learning (7) -- Simulation of target and small molecule interaction (semi flexible docking autodock)
教你自己训练的pytorch模型转caffe(二)
Maker education infiltrating the transformation of maker spirit and culture
【案例】元素的显示与隐藏的运用--元素遮罩
poj 3414 Pots (bfs+线索)
基于AVFoundation实现视频录制的两种方式