Skip to content

47 泛型类型 ​

Box<T> 定义一种结构,item 的类型由 T 决定。Box<Integer> 与 Box<String> 的字段类型分别受检查,值相等仍按内容比较。

norm
value Box<T> {
  T item
}

main() {
  Box<Integer> count = Box<Integer>(item: 7)
  Box<String> label = Box<String>(item: "Norm")
  printLine(count.item)
  printLine(label.item)
  printLine(count == Box<Integer>(item: 7))
}

预期输出:

text
7
Norm
true

动手试试:尝试把 label.item 赋给 Integer 变量。

精确规则:语言参考。

Norm 0.25