当前位置:网站首页>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
边栏推荐
- How to form standard interface documents
- Abnova DNA marker high quality control test program
- NPDP如何续证?操作指南来了!
- Document method
- Abnova丨 MaxPab 小鼠源多克隆抗体解决方案
- 王老吉药业“关爱烈日下最可爱的人”公益活动在南京启动
- Abbkine丨TraKine F-actin染色试剂盒(绿色荧光)方案
- 1. Strengthen learning basic knowledge points
- 如何让化工企业的ERP库存账目更准确
- Maker education infiltrating the transformation of maker spirit and culture
猜你喜欢
培养机器人教育创造力的前沿科技
Duchefa p1001 plant agar Chinese and English instructions
Chemical properties and application instructions of prosci Lag3 antibody
王老吉药业“关爱烈日下最可爱的人”公益活动在南京启动
Duchefa cytokinin dihydrozeatin (DHZ) instructions
Analysis of steam education mode under the integration of five Education
教你自己训练的pytorch模型转caffe(一)
[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
How to form standard interface documents
National Eye Care Education Conference, 2022 the Fourth Beijing International Youth eye health industry exhibition
随机推荐
Promouvoir le développement de l'industrie culturelle et touristique par la recherche, l'apprentissage et l'enseignement pratique du tourisme
[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
How to open an account online for futures? Is it safe?
Leetcode (695) - the largest area of an island
MySQL InnoDB架构原理
Duchefa low melting point agarose PPC Chinese and English instructions
鸿蒙os第四次学习
小程序项目结构
Abnova total RNA Purification Kit for cultured cells Chinese and English instructions
How to form standard interface documents
3.3、项目评估
Abnova丨DNA 标记高质量控制测试方案
PHP反序列化+MD5碰撞
Cutting edge technology for cultivating robot education creativity
Applet page navigation
Open source SPL eliminates tens of thousands of database intermediate tables
解读协作型机器人的日常应用功能
Go file path operation
Ros2 topic [01]: installing ros2 on win10
Applet event binding