当前位置:网站首页>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 Chinese and English instructions of spectinomycin hydrochloride pentahydrate
- Analysis of steam education mode under the integration of five Education
- POJ 3414 pots (bfs+ clues)
- Analyze the knowledge transfer and sharing spirit of maker Education
- 中国管理科学研究院凝聚行业专家,傅强荣获智库专家“十佳青年”称号
- Web Service简单入门示例
- Mode - "Richter replacement principle"
- XML建模
- Careercup its 1.8 serial shift includes problems
- Abnova blood total nucleic acid purification kit pre installed relevant instructions
猜你喜欢
线程池的使用
解读协作型机器人的日常应用功能
Which is the best online collaboration product? Microsoft loop, notion, flowus
渗透创客精神文化转化的创客教育
显示屏DIN 4102-1 Class B1防火测试要求
Graph embedding learning notes
EN 438-7建筑覆盖物装饰用层压板材产品—CE认证
ClickHouse 复制粘贴多行sql语句报错
中国的软件公司为什么做不出产品?00后抛弃互联网;B站开源的高性能API网关组件|码农周刊VIP会员专属邮件周报 Vol.097
王老吉药业“关爱烈日下最可爱的人”公益活动在南京启动
随机推荐
Is the securities account given by the school of Finance and business safe? Can I open an account?
判断横竖屏的最佳实现
Chemical properties and application instructions of prosci Lag3 antibody
显示屏DIN 4102-1 Class B1防火测试要求
Analysis of steam education mode under the integration of five Education
AITM2-0002 12s或60s垂直燃烧试验
Abnova total RNA Purification Kit for cultured cells Chinese and English instructions
基于flask写一个接口
2. < tag hash table, string> supplement: Sword finger offer 50 The first character DBC that appears only once
当Steam教育进入个性化信息技术课程
Norgen AAV提取剂盒说明书(含特色)
Analyze the knowledge transfer and sharing spirit of maker Education
Duchefa MS medium contains vitamin instructions
Écrire une interface basée sur flask
Duchefa cytokinin dihydrozeatin (DHZ) instructions
LeetCode: Distinct Subsequences [115]
教你自己训练的pytorch模型转caffe(一)
序列联配Sequence Alignment
Abnova CRISPR spcas9 polyclonal antibody protocol
当用户登录,经常会有实时的下拉框,例如,输入邮箱,将会@qq.com,@163.com,@sohu.com