当前位置:网站首页>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
边栏推荐
- Graph embedding learning notes
- Wanglaoji pharmaceutical's public welfare activity of "caring for the most lovely people under the scorching sun" was launched in Nanjing
- 【愚公系列】2022年7月 Go教学课程 004-Go代码注释
- Go file path operation
- 【UE4】UnrealInsight获取真机性能测试报告
- 2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc
- 研学旅游实践教育的开展助力文旅产业发展
- Research and development efficiency improvement practice of large insurance groups with 10000 + code base and 3000 + R & D personnel
- 2020 CCPC Weihai - A. golden spirit (thinking), D. ABC project (big number decomposition / thinking)
- 当Steam教育进入个性化信息技术课程
猜你喜欢

Cutting edge technology for cultivating robot education creativity

Abnova丨CRISPR SpCas9 多克隆抗体方案

Duchefa low melting point agarose PPC Chinese and English instructions

14、Transformer--VIT TNT BETR

Applet global configuration

Abbkine trakine F-actin Staining Kit (green fluorescence) scheme

【刷题记录】1. 两数之和

教你自己训练的pytorch模型转caffe(二)

Abnova CRISPR spcas9 polyclonal antibody protocol

Duchefa丨低熔点琼脂糖 PPC中英文说明书
随机推荐
渗透创客精神文化转化的创客教育
Informatics Olympiad 1340: [example 3-5] extended binary tree
The Chinese Academy of Management Sciences gathered industry experts, and Fu Qiang won the title of "top ten youth" of think tank experts
Duchefa丨P1001植物琼脂中英文说明书
【愚公系列】2022年7月 Go教学课程 004-Go代码注释
AI 从代码中自动生成注释文档
表单文本框的使用(二) 输入过滤(合成事件)
Make Jar, Not War
Go file path operation
CVPR 2022 | common 3D damage and data enhancement
When steam education enters personalized information technology courses
清除app data以及获取图标
Applet global configuration
Informatics Olympiad 1338: [example 3-3] hospital setting | Luogu p1364 hospital setting
Relationship between mongodb documents
1. Strengthen learning basic knowledge points
go 文件路径操作
PHP反序列化+MD5碰撞
鸿蒙os第四次学习
Ros2 topic [01]: installing ros2 on win10