我正在使用 Java 语言规范第三版 阅读 Java 中的泛型。在“4.6 Erasure”部分定义了类型删除。在删除类型变量时它说
The erasure of a type variable (§4.4) is the erasure of its leftmost bound.
这让我对类型变量和类型参数之间的区别有点困惑,因为“4.4 Type Variables”部分有定义:TypeParameter: TypeVariable TypeBound
其中绑定(bind)是可选的。但是也许你可以用它出现的类型参数来识别一个类型变量,因为一个类型变量只能(?)出现在一个“上下文”中,然后一个类型变量的最左边的边界被定义为其相应类型参数的最左边的边界或 Object
如果类型参数中没有显式绑定(bind)?
最佳答案
If no bound is given for a type variable, Object is assumed.
在您的链接中找到。这意味着给定 FirstClass<T extends String>
和 SecondClass<V>
你明白了:
- 类别:
FirstClass
类型参数:T extends String
.类型变量:T
.类型绑定(bind):String
. - 类别:
SecondClass
类型参数:V
类型变量:V
.类型绑定(bind):默认为Object
.
Edit: By Type Parameter, Type Variable and Type Bound 我不是指语法规则,但概念。因此,extends
只是关键字。
关于最左边的边界,你可以在同一个链接中找到答案,第一个引号后的两句话:
The order of types in a bound is only significant in that the erasure of a type variable is determined by the first type in its bound, and that a class type or type variable may only appear in the first position.
关于java - 类型变量和参数的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7075363/