TypeScript 四.类

类的创建和使用

class Person {
  //成员变量
  name: string;
  age: number;

  //构造函数
  constructor(myName: string, myAge: number) {
    this.name = myName;
    this.age = myAge;
  }

  //成员方法
  fun() {
    //方法体
    console.log("我的姓名" + this.name);
    console.log("我的年龄" + this.age);
  }
}

let person = new Person('xioaming',18)

 

posted @ 2021-11-19 18:44  lyzhi  阅读(37)  评论(0编辑  收藏  举报