概念

代码

  • 声明命名空间
  • 创建接口
  • 创建类
  • 实现接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
export namespace PersonMoudle {
interface Personface {
name: string;
age: number;
gender: string;
weat: string;
think(val: string): void;
}
export class Person implements Personface {
public name: string = null;
public age: number = null;
public gender: string = null;
public weat: string = null;

constructor(name, age, gender, weat) {
this.name = name;
this.age = age;
this.gender = gender;
this.weat = weat;
}
public think(val) {
console.log(`at just moment, i am think about:${val}`);
}
}
}

调用:

1
2
3
4
5
// import * as Person from "./person";
import { PersonMoudle } from "./person";
const { Person } = PersonMoudle;
const people = new Person("gaocaipeng", 25, "male", "140");
people.think("吃好吃的");