多个嵌套通配符 - 参数不适用 [英] multiple nested wildcard - arguments not applicable
问题描述
我已经大大简化了我的问题。
我想知道为什么下面的代码不能编译:
List< AnonType< AnonType<?>>>> l = new ArrayList< AnonType< AnonType<?>>>();
l.add(new AnonType< AnonType< String>>());
其中
public class AnonType< T> {
T a;
List< T> b;
}
编译器错误说add不适用于给定的参数。 OTOH,下面的代码只有一级嵌套通配符完全编译:
List< AnonType<?> l = new ArrayList< AnonType<?>>();
l.add(new AnonType< String>());
以下编译符合预期:
列表< Set< ;? extends Set<?>>> list = new ArrayList< Set< ;? extends Set<?>>>();
list.add(new HashSet< Set< String>>());
list.add(new HashSet< Set< Integer>>());
问题是泛型是类型不变的。
考虑一个更简单的例子:
- 假设从
Animal
到Dog
(例如Dog extends Animal
)...
- A
列表<动物>
不是 a列表< Dog>
- A
- 从
列表< ;? extends Animal>
p>现在这里会发生这种情况: - 假设有
Set<?> ;
到设置< String>
...
- c $ c>设置< Set<?>> 不是 a
Set< Set< String>>
- c $ c>设置< Set<?>> 不是 a
- 从
Set< ;? extends Set<?>
为Set< Set< String>>
- 无法转换为非特定嵌套类型为泛型
-
- 一个通用方法上的多个通配符使得Java编译器//stackoverflow.com/questions/746089/java-generic-listlist-extends-number\"> Java Generic
List< List< ;? extends Number>>
- 任何简单的方法来解释为什么我不能做
List< Animal> animals = new ArrayList< Dog>()
?
< E extends Number>
和< Number>
?
- 一个通用方法上的多个通配符使得Java编译器//stackoverflow.com/questions/746089/java-generic-listlist-extends-number\"> Java Generic
所以如果你想要一个 List< T>
,你可以添加 Set< Set< String> ; ,
设置< Set< Integer>>
em> NOT 设置< Set<?>>
,而是 Set< ;? extends Set<?>
相关问题>
另请参阅
I've heavily simplified my problem. Here's how it reads.
I'm trying to figure out why the following code does not compile:
List<AnonType<AnonType<?>>> l = new ArrayList<AnonType<AnonType<?>>>();
l.add( new AnonType<AnonType<String>>() );
where
public class AnonType<T> {
T a;
List<T> b;
}
The compiler error is saying that add is not applicable for the argument given. OTOH, the following code with only 1-level nested wildcard compiles perfectly:
List<AnonType<?>> l = new ArrayList<AnonType<?>>();
l.add( new AnonType<String>() );
The following compiles as expected:
List<Set<? extends Set<?>>> list = new ArrayList<Set<? extends Set<?>>>();
list.add(new HashSet<Set<String>>());
list.add(new HashSet<Set<Integer>>());
The problem is that generics is type invariant.
Consider the simpler example:
- Given that there is a casting conversion from
Animal
toDog
(e.g.Dog extends Animal
)...- A
List<Animal>
IS NOT aList<Dog>
- A
- There is a capture conversion from
List<? extends Animal>
to aList<Dog>
Now here's what happens in this scenario:
- Given that there is a capture conversion from
Set<?>
toSet<String>
...- A
Set<Set<?>>
IS NOT aSet<Set<String>>
- A
- There is a capture conversion from
Set<? extends Set<?>>
toSet<Set<String>>
So if you want a List<T>
where you can add a Set<Set<String>>
, Set<Set<Integer>>
, etc, then T
is NOT Set<Set<?>>
, but rather Set<? extends Set<?>>
.
Related questions
- Can't cast to to unspecific nested type with generics
- Multiple wildcards on a generic methods makes Java compiler (and me!) very confused
- Java Generic
List<List<? extends Number>>
- Any simple way to explain why I cannot do
List<Animal> animals = new ArrayList<Dog>()
? - What is the difference between
<E extends Number>
and<Number>
?
See also
这篇关于多个嵌套通配符 - 参数不适用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!