Java – cannot convert to non-specific nested types using generics

I have two nested generic classes Is there any way to get rid of it

Type mismatch: cannot convert from MSG < value < string > > to MSG < value > Wrong? On the last mission

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;
    }
}

Solution

Use the following:

Msg<? extends Value<?>> someMsg = strMsg;

The question is,? In MSG < value > Objmsg failed to capture the transformation It is not "some kind of value information. It is" any kind of value information "

This also explains why I also rename the variable somemsg. As the declaration changes Value cannot be just any object It must be of some type (string in this case)

A more general example

Let's consider list < < list > A more general example of Similar to the original scene, list < list > Cannot capture transformation 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"

This is another way to look at it:

>Java generics are type invariant > there is a conversion from integer to number, but a list < integer > is not a list < number >

>Similarly, list < integer > can be accessed through list Capture conversion, but list < list < integer > > is not a list < list >

>Use bounded wildcards, list can capture conversion list < integer >

>Similarly, the list > You can capture the transformation list < list < integer > >

Some facts? The following fragments can be captured and cannot be interpreted by others:

List<List<?>> lolAnything = new ArrayList<List<?>>(); // compiles fine!

    List<?> listSomething = new ArrayList<?>(); // DOES NOT COMPILE!!!
        // cannot instantiate wildcard type with new!

Related issues

> Multiple wildcards on a generic methods makes Java compiler (and me!) very confused

>This problem has been explored in great detail

> Java Generic List > > Any simple way to explain why I cannot do List

animals = new ArrayList

()? > What is the difference between

and

?



You can also have a look

> Java Generics Tutorial

> Generics and Subtyping | Wildcards | More Fun with Wildcards

> Angelika Langer’s Java Generics FAQ

> What is a bounded wildcard? > Which super-subtype relationships exist among instantiations of generic types?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>