In the Java SE1.5, a new feature is added: generics (generic in Japanese). What is generics? In layman's terms, it is a generic type of specified object that does not use a fixed type to specify as usual. The essence of generics is to parameterize the data type being manipulated, that is, the data type is specified as a parameter. This type of parameter can be used in classes, interfaces, and method definitions.
why the use of generics?
In previous J2SE, when there was no generics, the object type was usually used to manipulate multiple types of data. At this point, the most operation is the casting of data against the object, which is based on the fact that the developer is explicit about the data type (for example, converting an object to a string type). If the type is inconsistent, the compiler will not make an error during compilation, but it will fail at run time.
The advantage of using generics is that it carries out type security checks at compile time, and all transformations are enforced and implicit at run time, greatly increasing the reuse rate of the code.
ii. Definition & Use
The naming style for the type parameter is:
It is recommended that you use a concise name as the name of the form type parameter (if possible, a single character). It's best to avoid lowercase letters, which makes it and other ordinary
The formal parameters are easily distinguishable.
Use T to represent a type, no matter when there is no more specific type to differentiate it. This is often found in generic methods. If there are multiple type parameters, we
May use the adjacent letters of T in the alphabet, such as S.
If a generic function appears inside a generic class, it is best to avoid using the same name in the type parameters of the method and the type parameters of the class to avoid mixing
Xiao The same is true for inner classes.
1. Define a class with type parameters
When you define a class with a type parameter, you specify the name of one or more type parameters in the <> immediately following the class, and you can also take the type parameter
The value range is qualified, separated by the number of type parameters.
After you define a type parameter, you can use the type parameter almost anywhere in the class after the location is defined (except for static blocks, static properties, and static methods).
Just like using a normal type.
Note that the type parameters defined by the parent class cannot be inherited by the quilt class.
public class testclassdefine<t, S extends t> {
...
}
2. Define the method of parameter to be type
when defining a method with a type parameter, specify the name of one or more type parameters in <> immediately after the visible range decoration (for example, public).
It is also possible to qualify the range of a type parameter, separated by a number of types parameters.
After you define a type parameter, you can use the type parameter anywhere in the method after the location is defined, just as you would use a normal type.
For example:
Public <t, S extends t> t Testgenericmethoddefine (T T, s s) {
...
}
Note: You define a method with type parameters, the main purpose of which is to express the relationship between multiple parameters and the return value. For example, the following of T and S in this example
Inheritance, the type of the return value is the same as the value of the first type parameter.
If you just want to implement polymorphism, use the wildcard character as a priority. The contents of the wildcard characters are shown in the following sections.
Public <T> void TestGenericMethodDefine2 (list<t> s) {
...
}
should read
public void TestGenericMethodDefine2 (LIST<? > s) {
...
}
3. The type parameter assigns the value
when assigning a type parameter to a class or method, all type parameters are required to be assigned. Otherwise, you will get a compilation error.
4. Assign a type parameter to a class with a type parameter
There are two ways to assign a type parameter to a class with a type parameter
First declares a class variable or instantiates it. For example
list<string> list;
List = new arraylist<string>;
Second inheriting class or implementing an interface. For example
public class Mylist<e> extends arraylist<e> implements list<e> {...}
5. Assigning values to a method with type parameters
when a generic method is invoked, the compiler automatically assigns the type parameter, and when the time cannot be successfully assigned the Times compile error. For example
Public <T> T TestGenericMethodDefine3 (T T, list<t> List) {
...
}
Public <T> T testGenericMethodDefine4 (list<t> list1, list<t> list2) {
...
}
Number n = null;
Integer i = null;
Object o = null;
Testgenericmethoddefine (n, i);//At this time t is number, S is integer
testgenericmethoddefine (o, i);//t is object, S is integer
list<number> list1 = null;
TestGenericMethodDefine3 (i, list1)//At this time t is number
list<integer> list2 = null;
TestGenericMethodDefine4 (List1, List2)/compile an error
6. Wildcard characters
in the two subsections above, you can assign an indeterminate value to a type parameter, in addition to a specific value for the type parameter. For example
List<?> unknownlist;
list<? Extends number> unknownnumberlist;
list<? Super Integer> Unknownbaselineintgerlist;
Note: In the Java Collection framework, for a container class that has an unknown type for a parameter value, you can only read the elements and not add elements like them.
Because its type is unknown, the compiler does not recognize that the type of the element being added is compatible with the type of the container, the only exception being null
List<string> liststring;
list<?> unknownList2 = liststring;
Unknownlist = UnknownList2;
liststring = unknownlist;//Compilation error
7. Array Paradigm
You can declare an array using a class with a generic parameter value, but you cannot create an array
List<integer>[] Ilistarray;
New arraylist<integer>[10];//compile-time error
Third, expand
1, extends statement
using the extends statement restricts the scope of the generic parameter to be applied. For example:
<t extends Collection>, the use of the generic parameter is all CALSS that implements the collection interface. If you pass in a <String> the program compiles an error.
2, Super statement
The super statement, like extends, restricts the scope of application of generic parameters. The difference is that super is a restricted generic parameter that can only be the upper parent class that specifies that class.
For example, <t Super List>, which indicates that the generic parameter can only be the upper parent class of the list and list.
3. Wildcard characters
wildcard characters are used to resolve generic parameters that are limited to the disadvantage of not being able to dynamically determine based on an instance.
For example: public class SampleClass < T extends s> {...}
If A,b,c, ... Z These 26 classes all implement the S interface. We need to use the generic parameters of these 26 class types when we use them. What happens when it's instantiated? Write in sequence
sampleclass<a> A = new SampleClass ();
Sampleclass<b> a = new SampleClass ();
... Sampleclass<z> a = new SampleClass ();
This is obviously very redundant, rather than using the generic type object, huh?
Don't worry, we'll use the wildcard, OK.
sampleclass<? Extends s> sc = new SampleClass ();
Only need to declare a SC variable, very convenient to put!