java - 了解捕获的转化

标签 java generics

我想了解捕获的转换如何适用于通配符类型。 JLS里面有一段解释说:

Let G name a generic type declaration (§8.1.2, §9.1.2) with n type parameters A1,...,An with corresponding bounds U1,...,Un.

There exists a capture conversion from a parameterized type G<T1,...,Tn> (§4.5) to a parameterized type G<S1,...,Sn>, where, for 1 ≤ i ≤ n :

  • If Ti is a wildcard type argument (§4.5.1) of the form ?, then Si is a fresh type variable whose upper bound is Ui[A1:=S1,...,An:=Sn] and whose lower bound is the null type (§4.1).

  • If Ti is a wildcard type argument of the form ? extends Bi, then Si is a fresh type variable whose upper bound is glb(Bi, Ui[A1:=S1,...,An:=Sn]) and whose lower bound is the null type.

    glb(V1,...,Vm) is defined as V1 & ... & Vm.

    It is a compile-time error if, for any two classes (not interfaces) Vi and Vj, Vi is not a subclass of Vj or vice versa.

  • If Ti is a wildcard type argument of the form ? super Bi, then Si is a fresh type variable whose upper bound is Ui[A1:=S1,...,An:=Sn] and whose lower bound is Bi.

  • Otherwise, Si = Ti.

我不清楚的是Ui[A1:=S1,...,An:=Sn] .这是什么意思?我找不到通过 JLS 搜索的定义。

最佳答案

首先:定义替换。

[A<sub>1</sub>:=S<sub>1</sub>,...,A<sub>n</sub>:=S<sub>n</sub>]substitution in JLS notation (§1.3) ,定义为:

The notation [F<sub>1</sub>:=T<sub>1</sub>,...,F<sub>n</sub>:=T<sub>n</sub>] denotes substitution of F<sub>i</sub> by T<sub>i</sub> for 1 ≤ i ≤ n.

声明:[A<sub>1</sub>:=S<sub>1</sub>,...,A<sub>n</sub>:=S<sub>n</sub>]是类型参数(属于G)A<sub>i</sub>的替换按类型变量 S<sub>i</sub>对于 1 ≤ i ≤ n . (另见脚注 )

下一步:考虑为什么我们可能需要在第一种情况下进行替换。来自 §5.1.10 (突出显示添加到下一个点):

Let G name a generic type declaration ... with n type parameters A<sub>1</sub>,...,A<sub>n</sub> with corresponding bounds U<sub>1</sub>,...,U<sub>n</sub>.

即:

  • 界限U<sub>1</sub>,...,U<sub>n</sub> 具体对应类型参数A<sub>1</sub>,...,A<sub>n</sub> .
  • 未说明的推论:边界 U<sub>1</sub>,...,U<sub>n</sub>不对应类型变量 S<sub>1</sub>,...,S<sub>n</sub> .

放在一起:我现在说的是显而易见的,因为我确信你领先于我,但是......

关于“ T<sub>i</sub> 是一个通配符类型参数 ... 形式为 ? ”的规则

S<sub>i</sub> is a fresh type variable whose upper bound is U<sub>i</sub> [A<sub>1</sub>:=S<sub>1</sub>,...,A<sub>n</sub>:=S<sub>n</sub>]

  • S<sub>i</sub>是新类型变量
  • 其上限为U<sub>i</sub>
  • 哪里(这是隐含位)
  • 类型参数A<sub>1</sub>,...,A<sub>n</sub>U<sub>1</sub>,...,U<sub>n</sub>具体对应
  • 已被类型变量S<sub>1</sub>,...,S<sub>n</sub>替代
  • 制作U<sub>1</sub>,...,U<sub>n</sub>对应于类型变量 S<sub>1</sub>,...,S<sub>n</sub>

长话短说

即这只是意味着原来的上限U<sub>i</sub>也是 S<sub>i</sub> 的上限

脚注

有助于记忆from the JLS类型变量和类型参数不是一回事(因此我们可以开始明白为什么我们需要替换):

A type variable is an unqualified identifier used as a type in class, interface, method, and constructor bodies.

A type variable is introduced by the declaration of a type parameter of a generic class, interface, method, or constructor.

关于java - 了解捕获的转化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31206947/

相关文章:

java - SharedPreferences 在应用程序操作期间发生冲突,然后在应用程序强制关闭/重新启动设备后完全消失的问题

java - 安卓代码质量

java - 为什么这个简单的 Java 通用函数不能编译?

java - 使用 Jackson 将列表序列化为 xml,无需注释?

java - 通用 ConstraintViolations 集的异常

java - 单词出现方法不计数

java - 从 ObjectStream 读取 java.sql.Timestamp 对象而不更改时间值?

java - 在 Spring/Java 中过滤特殊字符

java - 运行时错误 : Array of Lists with generics in Java

java - Java 中观察者模式和泛型的问题