当前位置:网站首页>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
- [UE4] unrealinsight obtains the real machine performance test report
- Monorepo管理方法论和依赖安全
- 1、强化学习基础知识点
- 2020 CCPC Weihai - A. golden spirit (thinking), D. ABC project (big number decomposition / thinking)
- 解析创客教育的知识迁移和分享精神
- 3.3、项目评估
- Rainbow 5.7.1 supports docking with multiple public clouds and clusters for abnormal alarms
- The Chinese Academy of Management Sciences gathered industry experts, and Fu Qiang won the title of "top ten youth" of think tank experts
- 欢迎来战,赢取丰厚奖金:Code Golf 代码高尔夫挑战赛正式启动
猜你喜欢

Abnova丨荧光染料 620-M 链霉亲和素方案

Wanglaoji pharmaceutical's public welfare activity of "caring for the most lovely people under the scorching sun" was launched in Nanjing

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

小程序页面导航

Abnova blood total nucleic acid purification kit pre installed relevant instructions

Use of form text box (II) input filtering (synthetic event)

2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc

Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs

Applet global configuration

Duchefa s0188 Chinese and English instructions of spectinomycin hydrochloride pentahydrate
随机推荐
Kubernetes resource object introduction and common commands (V) - (configmap & Secret)
小程序代码的构成
National Eye Care Education Conference, 2022 the Fourth Beijing International Youth eye health industry exhibition
Model method
Interpreting the daily application functions of cooperative robots
AI 从代码中自动生成注释文档
Dry goods navigation in this quarter | Q2 2022
Ros2 topic [01]: installing ros2 on win10
珍爱网微服务底层框架演进从开源组件封装到自研
Abnova fluorescent dye 620-m streptavidin scheme
Relationship between mongodb documents
资源道具化
Abbkine丨TraKine F-actin染色试剂盒(绿色荧光)方案
[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
Norgen AAV提取剂盒说明书(含特色)
王老吉药业“关爱烈日下最可爱的人”公益活动在南京启动
Leetcode (347) - top k high frequency elements
2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc
Mathematical analysis_ Notes_ Chapter 9: curve integral and surface integral
Monorepo管理方法论和依赖安全