You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

23 lines
479 B

2 years ago
  1. // 第一种
  2. class GenericNumber<NumType> {
  3. defaultValue: NumType
  4. add: (x: NumType, y: NumType) => NumType
  5. }
  6. // 明确指定 <类型>
  7. const myNum = new GenericNumber<number>()
  8. myNum.defaultValue = 10
  9. // 第二种
  10. class GenericNumber2<NumType> {
  11. defaultValue: NumType
  12. add: (x: NumType, y: NumType) => NumType
  13. constructor(value: NumType) {
  14. this.defaultValue = value
  15. }
  16. }
  17. // 省略类型
  18. const myNum2 = new GenericNumber()
  19. myNum.defaultValue = 10