6.接口.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. (function() {
  2. type happy = {
  3. name: string,
  4. age: number,
  5. }
  6. // 类型别名 定义数据
  7. const obj:happy = {
  8. name: "孙悟空",
  9. age: 20,
  10. }
  11. /**
  12. * 接口 换而言之 也是一种定义数据的规范
  13. *
  14. */
  15. interface sad {
  16. name: string,
  17. age: number,
  18. }
  19. interface sad {
  20. sex: string
  21. }
  22. // 继承 extends
  23. // 接口 interface implements
  24. class Person implements sad {
  25. // num: number;
  26. // constructor(num: number) {
  27. // this.num = num;
  28. // }
  29. name: string;
  30. age: number;
  31. num: number;
  32. sex: string;
  33. constructor(name: string, age: number, sex: string,num:number) {
  34. this.name = name;
  35. this.age = age;
  36. this.num = num;
  37. this.sex = sex;
  38. }
  39. say() {
  40. console.log(this.name, this.age, this.num, this.sex,'我是个好人');
  41. }
  42. }
  43. let a = new Person("猪八戒", 20, "男",1);
  44. a.say()
  45. })()