无法转换为具有泛型的非特定嵌套类型 [英] Can't cast to to unspecific nested type with generics
问题描述
我有两个类与嵌套泛型。是否有办法摆脱
类型不匹配:无法从 Msg <值<字符串>< c $ c>到
错误? Msg< Value<?>
在最后一个作业
public class Value< V> {
V val;
public Value(V val){
this.val = val;
}
@Override
public String toString(){
return+ val;
}
}
public class Msg< T> {
T holder;
public Msg(T holder){
this.holder = holder;
}
public String toString(){
return+ holder;
}
public static void main(String [] args){
Msg< Value< String> strMsg = new Msg(new Value& ));
//这是OK
Msg<?> objMsg = strMsg;
//类型不匹配:不能从Msg转换< Value< String>>到Msg<值<>>
Msg< Value<?>>>> objMsg = strMsg;
}
}
以下内容:
Msg < extends Value<?>> someMsg = strMsg;
问题是,?
code> Msg< Value<?>> objMsg 是 能够捕获转换。这不是 类型的
$ c> $ c> Msg
这也解释了为什么随着声明的改变,我也将变量重命名为 someMsg
。 Value
不能只是任何 Object
。它必须属于某些类型( String
$ b 让我们考虑一个更加通用的例子: List< List<?>
。类似于原始场景, List< List<?> ;>
可以 捕获转换列表< List< Integer>>
。 List< List< Integer>> lolInt = null;
List< List& lolInt; // Do not NOT COMPILE !!!
//任何列表的列表
列表< ;? extends List<?>> lolSomething = lolInt; //编译好!
//某物列表列表
这是另一种查看方式:
- Java泛型是类型不变式
-
整数
改为 数字
,但列表<
整数
>
不是列表 Number
/ em> >
- 同样, code> List< Integer>
可以通过 code>
,但列表 列表<整数>
>
不是清单<
列表<?>
>
使用有界通配符,列表< ;? extends
Number
>
可以捕获转换列表<
整数
code>>
- 同样,
List < c c <> code>可以捕获转换
列表
列表<整数>
em>>
b $ b
某些?
可以捕获而其他人也不能解释下面的代码片段:
列表< List<?>> lolAnything = new ArrayList< List<?>>(); //编译好!
列表<?> listSomething = new ArrayList<?>(); //不能编译!
//不能用新的实例化通配符类型!
相关问题
- 多个通配符一个通用的方法使Java编译器(和我!)很困惑
- 很长时间详细探索这个问题
- Java通用
List< List< ;? extends Number>>
- 任何简单的方法来解释为什么我不能做
List< Animal> animals = new ArrayList< Dog>()
? -
< E extends Number>
和< Number>
?
另请参阅
I have two classes with nested generics. Is there a way to get rid of the
Type mismatch: cannot convert from Msg<Value<String>>
to Msg<Value<?>>
error ?
In the last assignment
public class Value<V> {
V val;
public Value(V val) {
this.val = val;
}
@Override
public String toString() {
return "" + val;
}
}
public class Msg<T> {
T holder;
public Msg( T holder) {
this.holder = holder ;
}
public String toString() {
return "" + holder;
}
public static void main(String[] args) {
Msg<Value<String>>strMsg = new Msg(new Value<String>("abc"));
// This is OK
Msg<?>objMsg = strMsg;
// Type mismatch: cannot convert from Msg<Value<String>> to Msg<Value<?>>
Msg<Value<?>>objMsg = strMsg;
}
}
Use the following:
Msg<? extends Value<?>> someMsg = strMsg;
The problem is that the ?
in Msg<Value<?>> objMsg
is NOT capable of capture conversion. It's not "a Msg
of Value
of some type. It's "a Msg
of Value
of ANY type".
This also explains why along with the declaration change, I've also renamed the variable to someMsg
. The Value
can't just be any Object
. It must belong to some type (String
in this example).
A more generic example
Let's consider a more generic example of a List<List<?>>
. Analogously to the original scenario, a List<List<?>>
can NOT capture-convert a List<List<Integer>>
.
List<List<Integer>> lolInt = null;
List<List<?>> lolAnything = lolInt; // DOES NOT COMPILE!!!
// a list of "lists of anything"
List<? extends List<?>> lolSomething = lolInt; // compiles fine!
// a list of "lists of something"
Here's another way to look at it:
- Java generics is type invariant
- There's a conversion from
Integer
toNumber
, but aList<
Integer
>
is not aList<
Number
>
- Similarly, a
List<Integer>
can be capture-converted by aList<?>
, but aList<
List<Integer>
>
is not aList<
List<?>
>
- Similarly, a
- Using bounded wildcard, a
List<? extends
Number
>
can capture-convert aList<
Integer
>
- Similarly, a
List<? extends
List<?>
>
can capture-convert aList<
List<Integer>
>
- Similarly, a
The fact that some ?
can capture and others can't also explains the following snippet:
List<List<?>> lolAnything = new ArrayList<List<?>>(); // compiles fine!
List<?> listSomething = new ArrayList<?>(); // DOES NOT COMPILE!!!
// cannot instantiate wildcard type with new!
Related questions
- Multiple wildcards on a generic methods makes Java compiler (and me!) very confused
- Very long and detailed exploration into this problem
- 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屋!