TS中类的修饰符

Posted 青袂婉梦

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TS中类的修饰符相关的知识,希望对你有一定的参考价值。

// public   公有  在类里面,子类 类外面都可以访问

// protected   保护类型  在类里面,子类 都可以访问  ,类外面无法访问

//provate    私有  在类里面,子类 类外面都不可以访问



//属性不加修饰符默认公有public   

class Person {
      public name: string //公有属性 public关键词可省略
      constructor(name: string) { 
      this.name = name;
}
getName(): string {
      return this.name;
}
setName(name: string): void {
      this.name = name;
}

}
var p = new Person(‘张三’);
console.log(p.getName());

p.setName(‘李四’);

console.log(p.getName());

以上是关于TS中类的修饰符的主要内容,如果未能解决你的问题,请参考以下文章