• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

private、public、protected、readonly、static

武飞扬头像
这是提莫大人
帮助4

1、默认是public

自由的访问程序里定义的成员

class Parent {
  public name: string;
  constructor(theName: string) {
    this.name = theName;
  }
}
let a = new Parent("张三").name;

2、private

私有,不能在声明它的类的外部访问,如下

class Parent {
  private name: string;
  constructor(theName: string) {
    this.name = theName;
  }
}
let a = new Parent("张三").name; //属性"name"为私有属性,只能在类"Parent"中访问。

学新通

3、protected

受保护成员只能在类及其子类中访问,而不能在类的实例中访问

class Parent {
  protected name: string;
  constructor(theName: string) {
    this.name = theName;
  }
}
let a = new Parent("张三").name;//属性"name"受保护,只能在类"Super及其子类中访问"

学新通

class Parent {
  protected name: string;
  constructor(theName: string) {
    this.name = theName;
  }
}
// let a = new Parent("张三").name;
class Cat extends Parent {
  constructor(theName: string) {
    super(theName);
    this.name = '张三,'
  }
}

4、readonly修饰符

class Octopsus {
    readonly name: string;
    readonly numberOfLegs: string;
    constructor(theName: string) {
        this.name = theName
    }
}
let dad = new Octopsus('张三');
dad.name = "修改值" //无法分配到"name",因为它是只读属性。

学新通

5、static

类的静态成员只能通过类名来调用,二不能通过子类调用

class Octopus {
    public name: string;
    constructor(theName: string) {
        this.name = theName
    }
    static food = "fish"
}
console.log(Octopus.food)

参考文档:https://typescript.bootcss.com/classes.html

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhefkkgg
系列文章
更多 icon
同类精品
更多 icon
继续加载