In Java? Extends T and? Super T
? Wildcard type
- <? Extends T> represents the upper bound of the type:
- A parameter indicative of the type may be T or a subclass of T
- <? Super T> represents the type of lower bound:
- A parameter indicating the type of this type is a supertype supertype i.e., until the Object
<? extends T>
- The upper bound of <? Extends T> can only be taken out, can not be kept inside
- Wildcard <?> And type parameter <T> difference:
- For the compiler, all represent the same type T
- Wildcard <?> Has no such constraints, only that placed a type, specifically what type can not be determined
- <? Extends T> role:
- List can not be used in the add method
- Initialization may specify different types of
- This allows different types of data read
<? super T>
-
<? Super T> data may be stored inside, taking out only on the object in the Object Data
- <? Super T> T can only add type and T type subclass, you can not add T type of parent, what can only be read out stored in the Object class
- The lower limit of the predetermined lower bound of the minimum size of elements, the type of container element relaxed control
- Because the element is T base class, so the deposit inside than T smaller are
- For the type of security considerations, can be added inside the object object of class T or any subclass of class T
-
Since the compiler does not know the contents of the collection from which a superclass of T, it is not allowed to join any specific super class object
-
When reading, the compiler does not know what type of case, can only return Object Object
- Because Object is the ultimate parent of any Java class
- At this time, the type of information elements are lost
PECS principle
- PECS原则: Producer Extends Consumer Super
- Frequent reading out the contents, suitable for use on community Extends
- Often inserted inside the content, use the lower bound for Super
to sum up
- extends may be used to define the type of return
- may be used to define the type of super parameters, return type is not defined for
- ? Neither method for passing parameters, the method can not be used for return
- Extends with wildcard subtype defined objects can be read to a generic
- Wildcard defined with super supertype may write to the generic object