当前位置: 代码网 > it编程>前端脚本>Vue.js > private、public、protected、readonly、static

private、public、protected、readonly、static

2024年08月01日 Vue.js 我要评论
private、public、protected、readonly、static

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

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com