java - 类型变量和参数的定义

标签 java generics definition

我正在使用 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>你明白了:

  1. 类别:FirstClass类型参数:T extends String .类型变量:T .类型绑定(bind):String .
  2. 类别: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/

相关文章:

java - Number 子类的防御性复制

C++ 内联类定义和对象初始化

java - JMockit:模拟接口(interface)时为 "Misplaced argument matcher detected here"

java - 了解 Android GC 消息

java - 服务工厂实例化的 bean 如何访问 httpServletRequest 对象?

java - 如何在子字符串之前和之后打印字符串,然后将新字符串添加到旧字符串中?

VB.NET 两种不同的通用跨线程操作方法;哪个更好?

java - 不了解 MapReduce NPE

variables - 通过 %let 定义 SAS 宏变量和调用 symput 之间的区别?

python - 如何启用定义输入作为可选?