TypeScript中创建类

TypeScript中创建类

  1. 需要向类中加入属性
  2. 然后传入构造方法constructor
  3. 再传入函数方法
class Person{
    // 属性
    name: string;
    age: number;
    // 构造方法
    constructor(name:string, age:number) {
        this.name = name;
        this.age = age;
    }
    // 函数
    print() {
        console.log(this.name + ' ' + this.age);
    }
}
// 必须传入参数
var p = new Person("Tracy", 26);
p.print()

猜你喜欢

转载自blog.csdn.net/weixin_42604536/article/details/85888081