Generic Generics
Parameterized types type parameterization
1. Definition of generics
Generics is a new feature of JDK 1.5. Its essence is parameterized type.
That is to say, the data type to be operated is specified as a parameter, and the specific type is specified when it is used.
This parameter type can be used in the creation of classes, interfaces and methods, which are called generic classes, generic interfaces and generic methods respectively.
A generic type can only be a reference type in the future, not a basic type.
For example:
//Compile and pass List<String> List<Action> List<Integer> List<Integer[]> List<int[]> //Compile and report errors List<int>
2. Generics in java only work during compilation, and the generics information will be erased at runtime.
It's just a matter of starting type safety checks during compilation, not at runtime.
For example: List list = new ArrayList();
Although the generic type is specified as String, other types of data can be stored in the list at runtime. (such as using reflection method)
//The following two methods are written in the class, and errors will be reported when compiling, because they are the same two methods after compiling, and they are the same method during running. public void test(List<String> list){ } public void test(List<Long> list){ }
3. Generic class
A generic class is a class with one or more type variables (parameterizing types)
To define a generic class, just add < > after the class name and type parameters in angle brackets.
Note: it is common for type variables to be capitalized and short.
In JDK, the variable E is used to represent the element type of the collection, and K and V are used to represent the type of key and value respectively. (other letters can be used if necessary, or one or more letters)
For example:
//The T in brackets depends on the type passed when the user uses the Point class. //For example: point < double > P = new point < double > public class Point<T> { private T x; private T y; public T getX() { return x; } public void setX(T x) { this.x = x; } public T getY() { return y; } public void setY(T y) { this.y = y; } }
For example:
//The T and S here are determined by the type passed by the user when using the Point class in the future. //Point<String,Integer> p = new Point<String,Integer>(); public class Point<T,S> { private T x; private S y; public T getX() { return x; } public void setX(T x) { this.x = x; } public S getY() { return y; } public void setY(S y) { this.y = y; } }
4. Generic interface
A generic interface is an interface with one or more type variables (type parameterization)
for example
//generic interface public interface Action<T,U>{ public void doSomeThing(T t,U u); } //Implementation class public class ActionImpl implements Action<String ,Date date>{ public void doSomeThing(String str,Date date){ System.out.println("On:"+Date+";We want"+str); } } //main ActionImpl a=new ActionImpl(); a.doSomeThing("Do homework",new Date());
4. Generic methods
Declare generics directly on methods, which are generic methods
Public void Run1 (T) {} This is a generic method
Public void dosomething (T, U) {0}; note that this is not a generic method, but a generic variable is used in the method
For example:
public class Test{ public <T> void run1(T t){ } public <T> T run2(T t){ return t; } public <T,S> void run3(T t,S s){ } }
6. Wildcard?
Generics increase the complexity of types in java, such as List, List, List and so on. (at compile time)
//Although List list = new ArrayList(); is correct //Although Object is the parent type of String //However, the following code is wrong when compiled, because generics are used List<Object> list = new ArrayList<String>();//Compile and report errors
Note: = the generics on both sides of the assignment operation are not polymorphic, that is, they are incompatible during compilation and are erased after compilation. Therefore, the generics on both sides of the assignment operation must be the same, otherwise an error is reported.
In generics? Is a wildcard, which can represent the parent types of all generics:
List<?> list = new ArrayList<Any type>();
For example:
//In this case, list can point to any generic list type collection object. public void test(List<?> list){ //Compile and report errors, because we don't know what type it represents, so we don't know what type 1 is. list.add(1); //Compile passed;? Represents the Object class for(Object o:list){ System.out.println(o); } }
7. Upper and lower limits in generics; entends and super keywords
In generics, you can use the entends and super keywords to represent the upper and lower limits of generic parameters passed by future users.
entends: indicates the upper limit, that is, the actual type of the generic passed in must be a type or its subclass
When declaring generic classes, interfaces, methods and variables, you can use the extends keyword to represent the upper limit.
super: indicates the lower limit, that is, the actual type of the generic passed in must be a type or its parent type
You cannot modify a generic class, interface, or method, and an error will be reported; you can only use the super keyword to indicate the lower limit when declaring a generic type variable
For example:
//extends can modify generic classes, interfaces, variables, methods //You can pass the Number class or its subclass public class Point<T extends Number>{ List<? extends Number>=new List<>; public <T extends Number> T test(T a){ } }
//super, only generic type variables can be decorated //You can pass the Number class or its parent class List<? super Number>=new List<>;
8) in generics&
Use & to add multiple restrictions to generics
For example:
public class A{ } public inerface B{ } //Whether the qualification is a class or an interface, the keyword extends is used uniformly. //Multiple restrictions can be given using the & symbol //If there is both an interface and a class, then there must be only one class and it must be placed in the first place public class Point<T extends A&B> { } class Sub extends A implements B{} main: //Compile error, must be type A and B or its subclass Point<A> p = new Point<A>(); Point<B> p = new Point<B>(); //Through compilation, Sub inherits A to implement B Point<Sub> p = new Point<Sub>();