ts中类的修饰符

public
定义为公有属性,类的内部,子类,外部都可以访问,默认为 public
外部访问必须使用new关键字

class A {
  public text: string = "公有属性public"
  getText() {
    return this.text
  }
}

const a = new A()
console.log(a.getText()) // 公有属性public
console.log(a.text) // 公有属性public

private
定义为私有属性,只能在类的内部访问,子类,外部访问会报错。

class A {
  private text: string = "私有属性private"
  constructor(text:any){
    this.text=text
  }
  getText() {
    return this.text
  }
}
class B extends A {
  constructor(text:any) {
    super(text)
  }
  addtext(){
    console.log(this.text)// 语法检查报错 子类不可访问父类的private
  }
}
const a = new A('a')
const b = new B('b')
console.log(a.text) // 语法检查报错 外部不可访问private
console.log(a.getText()) // 正常打印 通过方法转一下就可以访问到了,不算外部直接访问了
console.log(b.getText()) // 正常打印 通过方法转一下就可以访问到了,不算外部直接访问了

protected
定义为保护属性,可以在类,子类的内部可以访问,外部访问会报错。

class A {
  protected text: string = "保护属性正常打印"
  constructor(text:any){
    this.text=text
  }
  getText() {
    return this.text
  }
}
class B extends A {
  constructor(text:any) {
    super(text)
  }
  addtext(){
    console.log(this.text)// 正常打印 子类可以访问父类的protected
  }
}
const a = new A('a')
const b = new B('b')
console.log(a.text) // 语法检查报错 外部不可访问protected
console.log(a.getText()) //正常打印  通过方法转一下就可以访问到了,不算外部直接访问了
console.log(b.getText()) //正常打印  通过方法转一下就可以访问到了,不算外部直接访问了
b.addtext()// 正常打印 通过方法转一下就可以访问到了,不算外部直接访问了

static
定义为静态属性,函数内部只能在静态方法中使用,函数外可以直接以(函数名.属性名)的形式访问

class A {
  public text1: string = "公有属性public"
  protected text2: string = "保护属性protected"
  private text3: string = "私有属性private"
  static text4: string = "静态属性static"
  public getText2() {
    console.log(this.text1,this.text2,this.text3,this.text4)//text4编译报错,其他正常打印
  }
  protected getText3() {
    console.log(this.text1,this.text2,this.text3,this.text4)//text4编译报错,其他正常打印
  }
  private getText4() {
    console.log(this.text1,this.text2,this.text3,this.text4)//text4编译报错,其他正常打印
  }
  static getText1() {
    console.log(this.text1,this.text2,this.text3,this.text4)//text4正常打印,其他编译报错
  }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TypeScript中类修饰符有以下几种: 1. public public是默认修饰符,即不写修饰符时默认为public。public修饰的属性或方法可以在类的内部和外部使用。 2. private private修饰的属性或方法只能在类的内部使用,无法在类的外部使用。 3. protected protected修饰的属性或方法可以在类的内部和子类中使用,但无法在类的外部使用。 4. readonly readonly修饰的属性只能在初始化时或构造函数中赋值,之后就无法再修改。 示例代码: ```typescript class Person { public name: string; // 公共属性 private age: number; // 私有属性 protected gender: string; // 保护属性 readonly id: number; // 只读属性 constructor(name: string, age: number, gender: string, id: number) { this.name = name; this.age = age; this.gender = gender; this.id = id; } public sayHello() { // 公共方法 console.log(`Hello, my name is ${this.name}.`); } private getAge() { // 私有方法 return this.age; } protected getGender() { // 保护方法 return this.gender; } } class Student extends Person { public getGenderAndAge() { console.log(`My gender is ${this.getGender()} and age is ${this.getAge()}.`); } } const person = new Person('Tom', 18, 'male', 1001); console.log(person.name); // Tom // console.log(person.age); // 无法访问 // console.log(person.gender); // 无法访问 // person.id = 1002; // 无法修改 person.sayHello(); // Hello, my name is Tom. const student = new Student('Jerry', 17, 'female', 1002); // console.log(student.age); // 无法访问 // console.log(student.gender); // 无法访问 console.log(student.name); // Jerry console.log(student.id); // 1002 student.getGenderAndAge(); // My gender is female and age is undefined. ``` 在上面的示例代码中,我们定义了一个Person类,其中包含了公共属性、私有属性、保护属性、只读属性、公共方法、私有方法和保护方法。这些属性和方法都有不同的修饰符,可以在不同的场景下使用。同时,我们还定义了一个Student类,继承自Person类,可以访问Person类中的保护属性和保护方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值