typescript 中都有哪些修饰符,说明他们的作用?

类中成员的修饰符,用来描述类中成员(属性、构造函数、方法)的可访问性。

1、public修饰符
当我们写一个类时,如果没有添加修饰符,那么类中成员就会有默认的修饰符public,表示公共的,在任何位置都可以访问该成员,在外部如a.name,在子类的内部(通过this.name)

class Person{
       public name:string;
       public age:number;
       public constructor(name:string,age:number){
            this.name=name;
            this.age=age;
        }
       public say(){
            console.log(`my name is ${this.name}`)
        }
    }

var a=new Student('小明',23)

2、private修饰符
使用private修饰的成员
外部无法访问这个成员
子类中也无法访问这个成员

class Person{
       private name:string;
        age:number;
        constructor(name:string,age:number){
            this.name=name;
            this.age=age;
        }
        say(){
            console.log(`my name is ${this.name}`)
        }
    }
    class Student extends Person{
        constructor(name:string,age:number){
            super(name,age)
        }
        say(){
            super.say();
            console.log('呵呵',this.name)//在子类中使用private的属性将报错
        }
    }
    var a=new Student('小明',23)
    console.log(a.name)//在外部使用private的属性将报错

3、protected修饰符
使用protected修饰的成员
在外部无法访问这个成员
但是在子类中可以访问这个成员

class Person {
  protected name: string;

  constructor(name: string) {
    this.name = name;
  }
}

class Employee extends Person {
  private salary: number;

  constructor(name: string, salary: number) {
    super(name);
    this.salary = salary;
  }

  public getSalary() {
    return this.salary;
  }

  public introduce() {
    console.log(`My name is ${this.name}.`);
  }
}

const employee = new Employee("Bob", 5000);
console.log(employee.name); // 错误!无法访问受保护的成员 'name'
console.log(employee.getSalary()); // 输出: 5000
employee.introduce(); // 输出: My name is Bob.

在这个例子中,name 是使用 protected 修饰的。它可以在 Person 类及其子类中被访问,但不能在外部代码中被访问。

4、readonly修饰符
使用readonly修饰的成员
在外部不能修改
在类的普通方法中,也不能修改
只有在类的构造函数中才能进行修改赋值

class User{
  constructor(
    //可以访问,但是一旦确定不能修改
    readonly id: number,
    //自身和子类可以访问,但是不能外部修改
    protected username: string,
    //外部包括子类不能访问,也不可以修改
    private password: string
  ){
    this.id:number = id;
    this.username: string = username;
    this.password: string = password;
  }
   getInfo(){
    this.id;
    this.username;
    this.password;
  }
   //可以通过public方法修改private属性
  setPassword(password: string){
    if(password.length >= 6){
      this.password = password;
    }
  }
}
class Vip extends User{
  getInfo2(){
    this.id;
    this.username;
    this.password; //error
  }
}
let user1 = new User(1,"thia","123456");

user1.id;
user1.username; //error
user1.password; //error

5、构造函数中的参数属性
构造函数中的参数可以使用public、private、protected、readonly进行修饰,无论是哪个进行修饰,该类中都会自动添加这么一个属性成员。

与JS中书写类不一样,TS中书写类,在constructor构造函数中为属性赋值前,需要先在构造函数外定义属性

class Person {
        name:string;
        constructor(name:string){
            this.name=name
        }
    }

如果,在构造函数中的参数中使用public修饰符修饰参数,那么类中都会自动添加这么一个属性成员,其余修饰符也是一样。

 class Person {
        constructor(public name:string='小明'){       
        }
    }

总结

publicprivate 和 protected 这三种访问修饰符可以帮助我们控制类成员的访问权限。我们了解了它们的用法和作用范围,以及如何在类的实例化和继承过程中使用它们。

希望能够帮助大家更好地掌握 TypeScript 中的访问修饰符,并在实际开发中灵活运用。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值