Java generics <? Extends T> and <? The difference between super T>, extendssuper
For Java generics, I don't want to summarize what it is here. This Baidu has a lot of explanations, and there are also clear definitions in various java books. You just need to take a look at it and it will be clear soon. the English name of the Generic model, Generic type, can also be seen that the General, General, and general Generic is a general word, so the Generic model can be understood from the name, it is a general type and a summary of various types in java.
? Is a wildcard in java generics, which represents a class in java, then <? Extends T> represents a subclass of type T. <? Super T> represents a parent class of type T.
Here we first define a group of classes with inheritance relationships:
// Sub-class --> parent class small red apple -- Apple -- fruit -- delicious -- eat
These classes are the classes on the left that are connected to it.
So <? Extends Apple> represents a class in the left-side blue text and green text, and <? Super Apple> represents a class in the green and red letters.
Note that <? Extends T> or <? Super T> indicates a specific class in the range, rather than all classes in the range.
// As long as the value is within the range, we can assign the List as follows: <? Extends Apple> list1 = new ArrayList <Apple> (); List <? Extends Apple> list2 = new ArrayList <red apple> (); List <? Extends Apple> list3 = new ArrayList <red apple> ();
But for List <? Extends Apple> list indicates a class in a range, but it is not sure which class it is. If we add elements to this list:
List <? Extends Apple> list = new ArrayList <Apple> (); list. add (Apple); // compilation Error list. add (Red Apple); // compilation Error list. add (Red Apple); // compilation Error
Because the compiler does not know which class the list belongs to (only when running can it determine which class to refer to). If the list is red apple, then list. add (Apple) assigns a parent class to the subclass, which is incorrect. obviously, adding classes to this list cannot be guaranteed to be correct. it may be said that there is no subclass of Xiaohong apple and it will not be wrong to add Xiaohong apple, but this is just an inheritance diagram I have defined. We can continue to define a little red apple to inherit Xiaohong apple, this inheritance has no lower limit. this counterargument introduces a conclusion <? Extends T> is a type with a maximum of T, so we will immediately find <? Super T> actually a type with a lower limit of T.
Because for <? Extends T> has the upper limit of T. Therefore, if list. get (0) must return a subclass of T or T, this is OK and the result is:
List <? Extends Apple> list1 = new ArrayList <Apple> (); apple a = list1.get (0); // This must be true and compilation will not be problematic.
List <? Extends Apple> list2 = new ArrayList <red apple> (); apple a = list2.get (0 );
List <? Extends Apple> list3 = new ArrayList <red apple> (); apple a = list3.get (0 );
Then let's see <? Super T>, because it has a lower limit, we can immediately conclude that it is okay to add a T-type object to it. Because <? Super T> is a parent class of T. It is no problem to assign the subclass T to the parent class:
List <? Super Apple> list = new ArrayList <Apple> (); list. add (Apple); // No problem List <? Super Apple> list = new ArrayList <fruit> (); list. add (Apple); // No problem