Why do we need generics
1. Apply to multiple data types and execute the same code
2. After using generics, we can specify our data type in the coding process without forced type conversion. If we insert the wrong data type, we can find it when inserting.
Definition of generics
Parameterized type.
How to define your own generics
Generic classes, generic methods, generic interfaces... Are all OK.
Generic classes are as follows
public class NormalGeneric<T> { private T data; public NormalGeneric(){ } //Although generics are used in methods, this is not a generic method //This is just an ordinary member method in the class, but its return value is the declared generic of the declared generic class //Therefore, we can continue to use the generic type T in this method public T getData(){ return data; } /** This is a real generic method. First, the < T > between public and the return value is essential, which indicates that this is a generic method and declares a generic t This T can appear anywhere in the generic method The number of generics can also be any number public <T,K> K showKeyName(NormalGeneric<T> container){ ... } */ public void setData(T data){ this.data=data; } public static void main(String[] args){ NormalGeneric<String> normalGeneric=new NormalGeneric<>(); normalGeneric.setData("OK"); System.out.println(normalGeneric.getData()); } }
generic interface
public interface Genertor<T> { public T next(); }
How to implement it? There are two ways
public class ImplGenertor<T> implements Genertor<T> { @Override public T next() { return null; } }
The generic class above inherits the generic interface, which is not much different from the implementation of general generic classes. If you know the data type you need, you can directly implement it in the following ways.
public class ImplGenertor2 implements Genertor<String>{ @Override public String next() { return null; } }
It should be noted that generic methods can also exist in ordinary classes
public class GenericMethod { public <T> T genericMethod(T...a){ return a[a.length/2]; } public void test(int x,int y){ System.out.println(x+y); } public static void main(String[] args){ GenericMethod genericMethod=new GenericMethod(); genericMethod.test(23,123); System.out.println(genericMethod.<String>genericMethod("mark","av","lance")); System.out.println(genericMethod.genericMethod(12,34,45)); } }
The following example provides a deeper understanding of generics
public class GenericMethod3 { static class Fruit{ @Override public String toString(){ return "fruit"; } } static class Apple extends Fruit{ @Override public String toString(){ return "Apple"; } } static class Person{ @Override public String toString(){ return "Person"; } } static class GenerateTest<T>{ public void show_1(T t){ } /* A generic E is declared in the generic class. Use generic E, which can be of any type. Since generic methods declare generics < E > when declared, even if generics are not declared in generic classes The compiler can also correctly identify generics identified in generic methods */ public <E> void show_3(E t){ System.out.println(t.toString()); } /* Note that T here is a new type, which can not be the same type as T declared in the generic class */ public <T> void show_2(T t){ System.out.println(t.toString()); } } public static void main(String[] args){ Apple apple=new Apple(); Person person=new Person(); GenerateTest<Fruit> generateTest=new GenerateTest<>(); generateTest.show_1(apple); generateTest.show_2(person); generateTest.show_3(person); } }
Qualified type variable
How to ensure that there are certain methods or rules under the generic type of execution?
For example, in the following example, how can we guarantee that there will be compareTo?
Add the following inheritance relationship
public static <T extends Comparable> T min(T a,T b){ if(a.compareTo(b)>0) return a; else return b; }
This T represents the subtype of the binding type, and Comparable represents the binding type.
Note that this qualified type can be either a class or an interface, but if the class and interface are mixed, the class must be written in front, and there can only be one class.
public static <T extends ArrayList&Comparable> T min(T a, T b){ if(a.compareTo(b)>0) return a; else return b; }
Generic classes also apply.
Note: type variables cannot be instantiated
Type variables cannot be referenced in static fields or methods
Insert picture description here