Tag: The Nether OID should compile the object add generic Java generic type
1. People who have a basic knowledge of Java know a little about generics, understand where they often appear and how to use them.
On class: Class name <T> {} on method: Public <T> void Method name (T x) {}
I will not repeat it.
2. Generics are changing the type into a parameter to pass in, so that the types that can be used are diversified, and then the decoupling is realized.
Java because generics occur after 1.5, in order to maintain compatibility with previous versions, the Erase method is used to implement generics. So there are more restrictions on use than C + +.
Erase what is, in fact, a certain degree of disregard of the type parameter T, directly from the class where T is the parent of the T to erase (or transformation?) ) For example: I call
Generic method, passed the type parameter T into the method interior, if not at the declaration of the public T method name (T extends a parent class X) {} Such declaration, in my argument
After the number T and X are passed in, Java has an up-type erase, and the parameter x is treated directly as the object class, not the T I passed in. If I declare the upward bounds,
Limited to T extends Father, then the parameter x is erased to the Father class, which is used as a Father class.
That is, within any class or method that has generics, it cannot know its own generic parameters, just as a generic parameter is not passed into a class or method.
so why do generic parameters work on classes and methods?
As described in "Thinking in JAVA", erasure and transformation occur on the boundary, which means that the parameters we pass in are erased when entering the class or method.
But it was transformed into the t that we set up when we passed it.
In this case, we know that within a generic class or method, any operation that involves a specific type (that is, a subclass of the erased type) cannot be performed . For example:
New T (), or T.play () (play is a method of a subclass rather than a method of erasing a class), or T.method (T x), TMethod (K x)
3. The following example cannot be established in generics
list<number> list = new list<integer> ();
Because in the compiler it seems list<number> is not List<integer> 's parent class (I don't know the specific reason, who knows what to tell me next), although in our opinion number is
The parent of the Integer, then they make up the container list should have this inheritance relationship.
in order to implement this inheritance relationship, we can use wildcard characters?. This can be list< extends Number> this is a declaration that the internal members of this List inherit from
Number, which determines the upper bound of the list. So we can execute list< Extends number> list = new list<integer> ();
However, it is important to note that the generic type is the parameter type T, which specifies the type (what type of input you enter), so we can only enter it when executing list.add ().
Data of type T. We expanded the range of optional types after using the wildcard character, so our operation was also limited, for example: List.add (), because we can choose the real
There are too many instances of list, as long as the subclass of number is OK, Java can not know what we will instantiate the list, in order to ensure that security is not add. But we can
List.get (), Java will turn all we get into the number type output (the Richter substitution principle).
With the upper bound can have the lower bound, list< Super Integer>, as opposed to upper bounds. Because we don't know which integer parent we're going to be, we can't get anything,
But we can add in, as long as add is the integer and its subclass on the line (or the Richter principle).
And we can use LIST< extends t> This approach to further increase our choice.
4. There is also an unbounded wildcard <?>, which, based on the above inference, knows its role and limitations, rather than the non-generic list, which emphasizes that it is a list of certain classes, not an object
List of classes, but I'm not sure what class it is.
How Java generics are implemented