Home > Back-end >  About the generic class type assignment problem when using wildcards
About the generic class type assignment problem when using wildcards

Time:11-28

 
/*
* the known knowledge:
* assume G is a generic class, then to G And G , regardless of whether the class A and class B has inheritance relationships
* G
And G Are regarded as two independent of each other without any relationship types, namely G Object and G Object reference not each assignment
* like a Sting type object reference can't be assigned to a Date object, and vice versa
*
* but G
Object and G Object references can be assigned to G<?> Object
* because G<?> The equivalent of G
And G The parent class, G<?> Even can be regarded as a G The parent of the
*
* question:
* the following code, the first three lines are no problem, reference assignment operation comply with the above statement
*
* but for the fourth line of code, I just a layer of more nested generic
* but compile error: Type mismatch: always convert from Set The to Set
* the type can't convert
* by the first layer was specified generics are Map. The Entry should be compiled through, the second is on the left side of the generic G<?> The form of right is G Also accord with the above statement in the form of
* why an error?
*/
Map The map=new HashMap<> (a);
Set Characters=map1. EntrySet ();
Set<?> Set2=characters;

Set Set3=characters; An error//visit


  • Related