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.
24 lines
479 B
24 lines
479 B
// 第一种
|
|
class GenericNumber<NumType> {
|
|
defaultValue: NumType
|
|
add: (x: NumType, y: NumType) => NumType
|
|
}
|
|
|
|
// 明确指定 <类型>
|
|
const myNum = new GenericNumber<number>()
|
|
myNum.defaultValue = 10
|
|
|
|
|
|
// 第二种
|
|
class GenericNumber2<NumType> {
|
|
defaultValue: NumType
|
|
add: (x: NumType, y: NumType) => NumType
|
|
|
|
constructor(value: NumType) {
|
|
this.defaultValue = value
|
|
}
|
|
}
|
|
|
|
// 省略类型
|
|
const myNum2 = new GenericNumber()
|
|
myNum.defaultValue = 10
|