The nature of generics is to parameterize types (to control the types of parameters that are specifically restricted by the different types specified by generics without creating new types).

That is, in the use of generics, the data type of the operation is specified as a parameter, which can be used in classes, interfaces, and methods, called generic classes, generic interfaces, and generic methods, respectively.

A generic class

The most basic way to write generic classes is:

// Here T can be written as any identifier. Common parameters such as T, E, K, V are used to represent generics
When instantiating a generic class, you must specify the specific type of T
public class Generic<T>{ 
    // Key is a member variable of type T, whose type is externally specified
    private T key;

    public Generic(T key) { // The generic constructor parameter key is also of type T, whose type is externally specified
        this.key = key;
    }

    public T getKey(a){ // The generic method getKey returns a value of type T, the type of which is externally specified
        returnkey; }}Copy the code

Generic method

Generic classes specify the specific type of the generic when instantiating the class. A generic method specifies the specific type of the generic when calling the method.

Generic methods can be defined in ordinary or generic classes.

// The E of a generic class has nothing to do with the T of a generic method
class GenerateTest<E>{

/** * A basic introduction to generic methods *@paramThe generic argument * passed in by tClass@return<T> <T> <T> <T> <T> * 2) Only methods that declare <T> are generic, and member methods that use generics in a generic class are not generic. * 3) <T> indicates that the method will use the generic type T before you can use the generic type T in the method. * 4) Like the definition of generic classes, here T can be written as any identifier, common parameters such as T, E, K, V are often used to represent generics. * /
public <T> T genericMethod(Class<T> tClass)throws InstantiationException ,
  IllegalAccessException{
        T instance = tClass.newInstance();
        returninstance; }}Copy the code

Note: static methods in a generic class cannot access generics defined on the class; static methods must also be defined as generic methods

A generic interface

Generic interfaces are defined and used in much the same way as generic classes.

// Define a generic interface
public interface Generator<T> {
    public T next(a);
}

// Implement a generic without passing in a generic argument
class FruitGenerator<T> implements Generator<T>{
    @Override
    public T next(a) {
        return null; }}// When a generic argument is passed to implement a generic type
public class FruitGenerator implements Generator<String> {

    private String[] fruits = new String[]{"Apple"."Banana"."Pear"};

    @Override
    public String next(a) {
        Random rand = new Random();
        return fruits[rand.nextInt(3)]; }}Copy the code

Qualification of generic parameters

Generic parameters can be qualified with the extends keyword,

, where T should be a subtype of the binding type and BoundingType can be either a class or an interface.

/ / a generic class
public class Generic<T extends Number>{... }// Generic methods, when adding type restrictions, must add restrictions on the 
      
        between the permission declaration and the return value
      
public <T extends Number> T showKeyName(Generic<T> container){... }Copy the code

reference

Java generics, www.cnblogs.com/coprince/p/…

Liao Xuefeng Java tutorial

Java Core Technology Volume 1