Where does the Java spec say List<T> assigns to List<? super T>?
假设类
1 2 | List<A> x; List<? super B> y = x; |
就规范而言,这意味着
1 | List<A> <: List<? super B> |
号
但是Java 8规范的第4.10节定义了子类型关系作为直接超关系EDCOX1〔4〕的传递闭包,并且它定义了直接超关系,它是用一个有限函数来计算EDOCX1×5的一组超类型。在输入
问题:规范的哪一部分说明上述代码是合法的?
编译器代码的动机是,所以不足以理解为什么它是直观合法的,或者想出一个处理它的算法。由于Java中的泛型子类型问题是不可判定的,所以我希望处理与SPEC完全相同的情况,因此需要处理该情况的规范的一部分。
The direct supertypes of the parameterized type
C , where1,...,Tn> Ti
(1 ≤ i ≤ n) is a type, are all of the following:
D1 θ,...,Uk θ> , whereD1,...,Uk> is a direct supertype ofC and1,...,Tn> θ is the substitution[F1:=T1,...,Fn:=Tn] .
C , where1,...,Sn>Si containsTi (1 ≤ i ≤ n) (§4.5.1).
号
让1,...,Sn> = List super B>
包含关系在§;4.5.1中定义。类型参数和通配符:
A type argument
T1 is said to contain another type argumentT2 , writtenT2 <= T1 , if the set of types denoted byT2 is provably a subset of the set of types denoted byT1 under the reflexive and transitive closure of the following rules (where<: denotes subtyping (§4.10)):
? extends T <= ? extends S ifT <: S
? super T <= ? super S ifS <: T
T <= T
T <= ? extends T
T <= ? super T
号
通过第二颗子弹,我们可以看到
将列表分配给什么?超级B>实际上是什么意思?
考虑以下程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public class Generics { static class Quux { } static class Foo extends Quux { } static class Bar extends Foo { } public static void main(String... args) { List<Foo> fooList = new ArrayList<>(); // This is legal Java List<? super Bar> superBarList = fooList; // So is this List<? super Foo> superFooList = fooList; // However, this is *not* legal Java superBarList.add(new Quux()); // Neither is this superFooList.add(new Quux()); // Or this: superFooList.add(new Object()); // But this is fine superFooList.add(new Foo()); } } |
为什么会这样?首先,我们来谈谈捷豹路虎的说法
根据JLS,?4.5.1:
A type argument T1 is said to contain another type argument T2, written T2 <= T1, if the set of types denoted by T2 is provably a subset of the set of types denoted by T1 under the reflexive and transitive closure of the following rules (where <: denotes subtyping (§4.10)):
- ? super T <= ? super S if S <: T
- T <= ? super T
号
因此,t<=?super s如果s<:t.
…但这意味着什么?如果我不能添加一个
因此,将
进一步阅读:Angelikalanger一般性解释的相关章节