简体   繁体   English

无法使用通配符泛型类型向 Java 集合添加值

[英]Can't add value to the Java collection with wildcard generic type

Why this code does not compile ( Parent is an interface)?为什么这段代码不能编译( Parent是一个接口)?

List<? extends Parent> list = ...
Parent p = factory.get();   // returns concrete implementation
list.set(0, p);   // fails here: set(int, ? extends Parent) cannot be applied to (int, Parent)

It's doing that for the sake of safety.这样做是为了安全。 Imagine if it worked:想象一下,如果它有效:

List<Child> childList = new ArrayList<Child>();
childList.add(new Child());

List<? extends Parent> parentList = childList;
parentList.set(0, new Parent());

Child child = childList.get(0); // No! It's not a child! Type safety is broken...

The meaning of List<? extends Parent> List<? extends Parent>的含义List<? extends Parent> is "The is a list of some type which extends Parent . We don't know which type - it could be a List<Parent> , a List<Child> , or a List<GrandChild> ." List<? extends Parent>是“这是扩展Parent的某种类型的列表。我们不知道哪种类型 - 它可以是List<Parent>List<Child>List<GrandChild> That makes it safe to fetch any items out of the List<T> API and convert from T to Parent , but it's not safe to call in to the List<T> API converting from Parent to T ... because that conversion may be invalid.这使得它的安全抓取任何物品List<T>的API,并且将来自TParent ,但它不是安全调用List<T> API从转换ParentT ...因为转换可能无效的。

List<? super Parent>

PECS - "Producer - Extends, Consumer - Super". PECS - “生产者 - 扩展,消费者 - 超级”。 Your List is a consumer of Parent objects.您的ListParent对象的使用者。

Here's my understanding.这是我的理解。

Suppose we have a generic type with 2 methods假设我们有一个带有 2 个方法的泛型类型

type L<T>
    T get();
    void set(T);

Suppose we have a super type P , and it has sub types C1, C2 ... Cn .假设我们有一个超类型P ,它有子类型C1, C2 ... Cn (for convenience we say P is a subtype of itself, and is actually one of the Ci ) (为方便起见,我们说P是自身的一个子类型,实际上是Ci

Now we also got n concrete types L<C1>, L<C2> ... L<Cn> , as if we have manually written n types:现在我们还得到了n 个具体类型L<C1>, L<C2> ... L<Cn> ,就好像我们手动编写了n 个类型:

type L_Ci_
    Ci get();
    void set(Ci);

We didn't have to manually write them, that's the point.我们不必手动编写它们,这就是重点。 There are no relations among these types这些类型之间没有关系

L<Ci> oi = ...;
L<Cj> oj = oi; // doesn't compile. L<Ci> and L<Cj> are not compatible types. 

For C++ template, that's the end of story.对于 C++ 模板,这就是故事的结尾。 It's basically macro expansion - based on one "template" class, it generates many concrete classes, with no type relations among them.它基本上是宏扩展——基于一个“模板”类,它生成许多具体的类,它们之间没有类型关系。

For Java, there's more.对于 Java,还有更多。 We also got a type L<? extends P>我们还有一个类型L<? extends P> L<? extends P> , it is a super type of any L<Ci> L<? extends P> ,它是任何L<Ci>的超类型

L<Ci> oi = ...;
L<? extends P> o = oi; // ok, assign subtype to supertype

What kind of method should exist in L<? extends P> L<? extends P>应该存在什么样的方法L<? extends P> L<? extends P> ? L<? extends P> ? As a super type, any of its methods must be hornored by its subtypes.作为一个超类型,它的任何方法都必须由它的子类型来支持。 This method would work:这种方法会起作用:

type L<? extends P>
    P get();

because in any of its subtype L<Ci> , there's a method Ci get() , which is compatible with P get() - the overriding method has the same signature and covariant return type.因为在它的任何子类型L<Ci> ,都有一个方法Ci get() ,它与P get()兼容 - 覆盖方法具有相同的签名和协变返回类型。

This can't work for set() though - we cannot find a type X , so that void set(X) can be overridden by void set(Ci) for any Ci .但这对set()不起作用 - 我们找不到类型X ,因此对于任何Civoid set(X)可以被void set(Ci)覆盖。 Therefore set() method doesn't exist in L<? extends P>因此L<? extends P>中不存在set()方法L<? extends P> L<? extends P> . L<? extends P>

Also there's a L<? super P>还有一个L<? super P> L<? super P> which goes the other way. L<? super P>则相反。 It has set(P) , but no get() .它有set(P) ,但没有get() If Si is a super type of P , L<? super P>如果SiP的超类型,则L<? super P> L<? super P> is a super type of L<Si> . L<? super P>L<Si>的超类型。

type L<? super P>
    void set(P);

type L<Si>
    Si get();
    void set(Si);

set(Si) "overrides" set(P) not in the usual sense, but compiler can see that any valid invocation on set(P) is a valid invocation on set(Si) set(Si) “覆盖” set(P)不是通常意义上的,但编译器可以看到任何对set(P)的有效调用都是对set(Si)的有效调用

This is because of "capture conversion" that happens here.这是因为这里发生了“捕获转换”。

Every time the compiler will see a wildcard type - it will replace that by a "capture" (seen in compiler errors as CAP#1 ), thus:每次编译器会看到通配符类型 - 它会用“捕获”(在编译器错误中看到为CAP#1 )替换它,因此:

List<? extends Parent> list

will become List<CAP#1> where CAP#1 <: Parent , where the notation <: means subtype of Parent (also Parent <: Parent ).将成为List<CAP#1>其中CAP#1 <: Parent ,其中符号<:表示Parent子类型(也是Parent <: Parent )。

java-12 compiler, when you do something like below, shows this in action: java-12编译器,当您执行以下操作时,会显示此操作:

List<? extends Parent> list = new ArrayList<>();
list.add(new Parent());

Among the error message you will see:在错误消息中,您将看到:

.....
CAP#1 extends Parent from capture of ? extends Parent
.....

When you retrieve something from list , you can only assign that to a Parent .当您从list检索某些内容时,您只能将其分配给Parent If, theoretically, java language would allow to declare this CAP#1 , you could assign list.get(0) to that, but that is not allowed.如果从理论上讲,java 语言允许声明这个CAP#1 ,您可以将list.get(0)分配给它,但这是不允许的。 Because CAP#1 is a subtype of Parent , assigning a virtual CAP#1 , that list produces, to a Parent (the super type) is more that OK.因为CAP#1Parent子类型,所以将list产生的虚拟CAP#1分配给Parent (超类型)就更OK了。 It's like doing:这就像做:

String s = "s";
CharSequence s = s; // assign to the super type

Now, why you can't do list.set(0, p) ?现在,为什么你不能做list.set(0, p) Your list, remember, is of type CAP#1 and you are trying to add a Parent to a List<CAP#1> ;请记住,您的列表属于CAP#1类型,并且您正在尝试将Parent添加到List<CAP#1> that is you are trying to add super type to a List of subtypes, that can't work.那就是您试图将超类型添加到子类型列表中,这是行不通的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM