5.接口.ts 996 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. (function () {
  2. /**
  3. * 类型别名 接口区别
  4. * 接口:偏向 对象/结构的定义 可扩展 可合并
  5. * 类型别名:偏向于任意类型 联合/交叉类型 简单的别名
  6. * */
  7. // 接口:定义数据类型的规范
  8. // interface status = 1|2|3;
  9. // 接口声明
  10. interface xxx {
  11. name: string,
  12. age: number,
  13. address: string,
  14. sex: string
  15. }
  16. interface xxx {
  17. sex: string
  18. }
  19. // 类实现接口 需要继承
  20. // 类型约束
  21. class List implements xxx {
  22. name: string;
  23. age: number;
  24. address: string;
  25. // sex: string;
  26. x: string;
  27. constructor(name: string, age: number, address: string, sex: string, x: string) {
  28. this.name = name;
  29. this.age = age;
  30. this.address = address;
  31. this.sex = sex;
  32. this.x = x;
  33. }
  34. }
  35. let x = new List('图图', 3, '上海', '12');
  36. console.log(x);
  37. })()