无法转换为具有泛型的非特定嵌套类型 [英] Can't cast to to unspecific nested type with generics

查看:78
本文介绍了无法转换为具有泛型的非特定嵌套类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类与嵌套泛型。是否有办法摆脱



类型不匹配:无法从 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<?>(); //不能编译!
//不能用新的实例化通配符类型!



相关问题





另请参阅




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 to Number, but a List<Integer> is not a List<Number>
    • Similarly, a List<Integer> can be capture-converted by a List<?>, but a List<List<Integer>> is not a List<List<?>>
  • Using bounded wildcard, a List<? extends Number> can capture-convert a List<Integer>
    • Similarly, a List<? extends List<?>> can capture-convert a List<List<Integer>>

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

See also

这篇关于无法转换为具有泛型的非特定嵌套类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆