> Generics: Generic refers to the typed type of the parameter, which is commonly used in interfaces, classes, methods
> function: Used to determine the range of parameters, in writing code in advance to check the code error
> generic declaration, which gives the class declaration, and so on:
Class classname<t>{}
Class classname<k,v,o>{}
> General character:<?> General designation of all classes
> Qualifier: <t exteds Super Class >
1) Qualifier letters can only be limited once
2) When extends can only use capital letters, when super can only use "?"
3) You cannot use the Super qualifier symbol in a class.
4) The qualifier in the class can be used by the method.
5) When the interface is declared with a qualified symbol, its subclasses must also have.
Class Book<t extends number>{
void Go (stack<t> Stack) {
}
public void Go2 (STACK<? Super Number> Stack2) {
}
}
1. Parameter conversion >
Statement:
class Book<t extends number>{}
Reference:
Book<number> numbook=new book<number> ();
Book<integer> numbook2=new book<integer> ();
Explicit or implicit conversion of parameters at the same level is wrong
numbook= (book<number>) numBook2;
2. Compare and assign values:
Statement:
class compareclass<t>{
T A;
Compareclass (T t) {
this.a=t;
}
}
Comparison:
Compareclass<float> f=new compareclass<float> ();
Compareclass<integer> i=new compareclass<integer> ();
F==i? This result produces a compilation error;
Assignment value:
f=i;? A compilation error is also generated;
2. Type parameter array >
Statement:
T[] T;
Defined:
t[] t=new int[10];//compilation error
Modify:
T[] T;
Int[] In=new int[]{10,20};
T=in ;
3. Type parameters static use > Since type parameters are determined only when the type is instantiated,
As the following code, the type parameter as a static variable will cause a compilation error;
class person<k,v>{
static K name;
Static V age;
Static V GetName () {
return age;
}
}
Java Advanced-Generics