#

class Person {
  public name: string // 公共属性
  private _age: number // 私有属性修饰符 只能在类的内部调用
  protected weight: number // 可以再继承的类内部调用
  static tail: boolean // 静态属性
  readonly id: number // 类外部可以访问 但无法修改
  
  // 存取器 类似于 Object.defineProperty 的 set 与 get
  get fuillname(): string {
    return this.name
  }
  
  set fullnamne(val: string) {
    this.name += val
  }
  
  constructor(name) {
    this.name = name
  }
}
  • 当传入构造器的参数上制定publicprivate时,都会在实例上创建相应的变量并填充其值
class Student {
  constructor(private jack: Person) {}

  sayHello() {
    console.log(`hello ${this.jack.fullname}`)
  }
}