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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3716920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:08:29  来源:igfitidea点击:

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

javagenerics

提问by Sergey Mikhanov

Why this code does not compile (Parentis 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)

回答by Jon Skeet

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>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>." That makes it safe to fetch any items outof the List<T>API and convert from Tto Parent, but it's not safe to call into the List<T>API converting from Parentto T... because that conversion may be invalid.

的意思List<? extends Parent>是“这是一个扩展的某种类型的列表Parent。我们不知道是哪种类型——它可能是 a List<Parent>、aList<Child>或 a List<GrandChild>。” 这使得它的安全抓取任何物品出来的的List<T>,从API和转换TParent,但它不是安全地调用List<T>API从转换ParentT......因为转换可能是无效的。

回答by Bozho

List<? super Parent>

PECS - "Producer - Extends, Consumer - Super". Your Listis a consumer of Parentobjects.

PECS - “生产者 - 扩展,消费者 - 超级”。您ListParent对象的消费者。

回答by irreputable

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. (for convenience we say Pis a subtype of itself, and is actually one of the Ci)

假设我们有一个超类型P,它有子类型C1, C2 ... Cn。(为方便起见,我们说它P是自身的一个子类型,实际上是其中之一Ci

Now we also got nconcrete types L<C1>, L<C2> ... L<Cn>, as if we have manually written ntypes:

现在我们也得到了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 norelations 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. It's basically macro expansion - based on one "template" class, it generates many concrete classes, with no type relations among them.

对于 C++ 模板,这就是故事的结尾。它基本上是宏扩展——基于一个“模板”类,它生成许多具体的类,它们之间没有类型关系。

For Java, there's more. We also got a type L<? extends P>, it is a super type of any L<Ci>

对于 Java,还有更多。我们还有一个类型L<? extends P>,它是 any 的超类型 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>? As a super type, any of its methods must be hornored by its subtypes. This method would work:

应该存在什么样的方法L<? extends P>?作为一个超类型,它的任何方法都必须由它的子类型来支持。这种方法会起作用:

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. Therefore set()method doesn't exist in L<? extends P>.

但是这不起作用set()- 我们找不到 type X,因此void set(X)可以被void set(Ci)for any覆盖Ci。因此set()方法不存在于L<? extends P>.

Also there's a L<? super P>which goes the other way. It has set(P), but no get(). If Siis a super type of P, L<? super P>is a super type of L<Si>.

还有一个L<? super P>走另一条路。它有set(P),但没有get()。如果Si是 的超类型PL<? 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)

回答by Eugene

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 ofParent(also Parent <: Parent).

将成为List<CAP#1>where CAP#1 <: Parent,其中符号<:表示(also ) 的子类型ParentParent <: Parent

java-12compiler, 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. If, theoretically, java language would allow to declare this CAP#1, you could assign list.get(0)to that, but that is not allowed. Because CAP#1is a subtypeof Parent, assigning a virtual CAP#1, that listproduces, to a Parent(the super type) is more that OK. It's like doing:

当您从 中检索某些内容时list,您只能将其分配给 a Parent。如果从理论上讲,java 语言允许声明 this CAP#1,您可以分配list.get(0)给 that,但这是不允许的。因为CAP#1是一个亚型Parent,分配的虚拟CAP#1,即list产生,到Parent(超类型)是更多的行。这就像做:

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

Now, why you can't do list.set(0, p)? Your list, remember, is of type CAP#1and you are trying to add a Parentto a List<CAP#1>; that is you are trying to add super type to a List of subtypes, that can't work.

现在,你为什么不能做list.set(0, p)?请记住,您的列表属于类型,CAP#1并且您正在尝试将 a 添加Parent到 a List<CAP#1>;那就是您试图将超类型添加到子类型列表中,这是行不通的。