当前位置:网站首页>ts 之 属性的修饰符public、private、protect
ts 之 属性的修饰符public、private、protect
2022-07-05 20:44:00 【qq_46302247】
public修饰的属性可以在任意位置访问和修改
class Animal{
name: string //等同于 public name: string; public是默认修饰符
age: number
constructor(name: string, age: number) {
this.name = name
this.age = age
}
}
也可以换成如下写法:
class Animal{
constructor(public name: string, public age: number) {
}
}
const a = new Animal('tt', 19)
console.log(a);//Animal {name: 'tt', age: 19}
private修饰的属性是私有属性,私有属性只能在类的内部访问和修改,它的子类也无法访问和修改;它的实例也无法访问
class Animal{
private name: string
private age: number
constructor(name: string, age: number) {
this.name = name
this.age = age
}
}
如果想访问private修饰的属性,可以通过 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受保护的属性,只能在当前类,和它的子类中访问和修改,但是他们的实例都不能访问
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('小黑', 18)
dog.test()//18
边栏推荐
- Duchefa s0188 Chinese and English instructions of spectinomycin hydrochloride pentahydrate
- Abnova fluorescent dye 620-m streptavidin scheme
- 3.3 project evaluation
- Duchefa丨S0188盐酸大观霉素五水合物中英文说明书
- Abnova丨 MaxPab 小鼠源多克隆抗体解决方案
- go 文件路径操作
- 教你自己训练的pytorch模型转caffe(一)
- Mongodb/ document operation
- Abbkine丨TraKine F-actin染色试剂盒(绿色荧光)方案
- Cutting edge technology for cultivating robot education creativity
猜你喜欢

Abbkine丨TraKine F-actin染色试剂盒(绿色荧光)方案

Abnova丨E (DIII) (WNV) 重组蛋白 中英文说明书

ProSci LAG-3 重组蛋白说明书

10000+ 代码库、3000+ 研发人员大型保险集团的研发效能提升实践

Open source SPL eliminates tens of thousands of database intermediate tables

Prosci LAG-3 recombinant protein specification

haas506 2.0开发教程 - 阿里云ota - pac 固件升级(仅支持2.2以上版本)

IC popular science article: those things about Eco

CVPR 2022 | common 3D damage and data enhancement

Applet global configuration
随机推荐
Classic implementation of the basic method of intelligent home of Internet of things
Document method
3.3、项目评估
小程序全局配置
Analyze the knowledge transfer and sharing spirit of maker Education
CVPR 2022 | common 3D damage and data enhancement
手机开户股票开户安全吗?我家比较偏远,有更好的开户途径么?
资源道具化
Interpreting the daily application functions of cooperative robots
When steam education enters personalized information technology courses
2020 CCPC Weihai - A. golden spirit (thinking), D. ABC project (big number decomposition / thinking)
台风来袭!建筑工地该如何防范台风!
AI 从代码中自动生成注释文档
Applet project structure
Analysis of steam education mode under the integration of five Education
AI automatically generates annotation documents from code
Abnova maxpab mouse derived polyclonal antibody solution
中国的软件公司为什么做不出产品?00后抛弃互联网;B站开源的高性能API网关组件|码农周刊VIP会员专属邮件周报 Vol.097
Abnova CRISPR spcas9 polyclonal antibody protocol
[quick start of Digital IC Verification] 2. Through an example of SOC project, understand the architecture of SOC and explore the design process of digital system