For one is list<? Extends a reference to a t> type List1, which is actually a specific type that is not specified by a list1 reference, it is a seed class of T, but what kind of subclass is the compiler cannot determine, and therefore cannot be used to add objects, However, because you can be sure that any object in this list1 is at least type T, you can use Get () to return an object of type T.
And for one is list< The Super t> type references List2, which is actually a base class of T, but because it is not possible to determine which base class it is, the List2 uses get () to return only an object type, the original T type is lost, but because the parameter is already known to be a base class of T, So it is safe to add a subtype of T or T to it, but if you add any base class to it, you will get an error due to uncertainty (even if you add the object type).
1 ImportJava.util.*;2 3 Public classDemo_twentyeight {4 Static classGeneric1<t>{5 PrivateT Item;6 Public voidput (T item) {7 This. item=item;8 }9 }Ten One Static classGeneric2<t>{ A T Item; - T Get () { - returnitem; the } - } - Static<T>voidF1 (generic1<?SuperT>generic1,t Item) { - generic1.put (item); + } - Static<T>voidF2 (generic2<?extendsT>Generic2) { + generic2.get (); A } at Public Static voidMain (string[] args) { -Generic1<fruit> g1=NewGeneric1<fruit>(); -G1.put (NewApple ()); -G1.put (NewFruit ()); - //G1.put (New Object ()); //compilation does not pass -Generic2<fruit> g2=NewGeneric2<fruit>(); inFruit f1=g2.get (); - } to +}
A more detailed answer (written by a great God): http://zhidao.baidu.com/question/646868566975781205.html
Problems encountered in Java Learning (ii) generics in <? Extends t> and <? The difference between Super t>