JAVA knowledge point review of generics

Overview

  • Generics can solve the security problem of data types. Principle: When the class is declared, an identifier is used to indicate the type of an attribute in the class or the return value and parameter type of a method
  • Generics are new features added after JDK1.5

format

1. Generic class
  访问权限  class 类名称<泛型,泛型...>{
    
    
        属性
        方法
  }

public class GenericDemo0<T> {
    
    

    private T t;

    public T getT() {
    
    
        return t;
    }

    public void setT(T t) {
    
    
        this.t = t;
    }

    public static void main(String args[]) {
    
    
        GenericDemo0<Integer> i = new GenericDemo0<>();
        i.setT(1);
        System.out.println("-----泛型类--->" + i.getT());

        GenericDemo0<String> str = new GenericDemo0<>();
        str.setT("泛型类");
        System.out.println(str.getT());
    }
}

A type parameter declaration part is added after the class name.
The type parameter declaration part of a generic class contains one or more type parameters. The parameters are separated by
a comma . It is called a type variable and is used to specify one Identifier of generic type name

Object creation: class name <specific type> object name=new class name <specific type> ()

2. Generic interface
 interface 接口名称<泛型标识>{
    
    }

interface Gen<T> {
    
    
    T call(T t);
}

Add <T> after the interface name

3. Generic methods
 访问权限 <泛型标识> 返回类型 方法名称 ([泛型标识 参数名称]private static <T> void call(T t) {
    
    
   System.out.println("---泛型方法-->" + t);
 }

 private static <E> E[] generCall(E[] array) {
    
    
        System.out.println("---泛型方法-->" + Arrays.toString(array));
        return array;
 }

All generic method declarations have a type parameter declaration part (separated by angle brackets). The type parameter declaration part
can be used to declare the return value type before the method return type and can be used as the actual parameter obtained by the generic method. Type placeholder
Note that type parameters can only represent reference types, not primitive types

Type wildcard

Upper bound wildcard

? extends is called the upper bound wildcard, which makes Java generics covariant. extends limits the parent type of generic types, so it is called upper bound

List<? extends TextView> textViews = new ArrayList<TextView>(); // 本身
List<? extends TextView> textViews = new ArrayList<Button>(); // 直接子类
List<? extends TextView> textViews = new ArrayList<RadioButton>(); // 间接子类
  • Among them? is a wildcard, indicating that the generic type of this List is an unknown type
  • extends limits the upper bound of this unknown type, that is, the generic type must meet the restrictions of this extends: its scope is not only all direct and indirect subclasses, but also the parent class defined by the upper bound itself, which is TextView; it There is also the meaning of implements, that is, the upper bound here can also be an interface.
Nether wildcard

? super is called the lower bound wildcard, which makes Java generics contradictory. super restricts the subtypes of the wildcard ?, so it is called the lower bound.

List<? super Button> buttons = new ArrayList<Button>(); // 本身
List<? super Button> buttons = new ArrayList<TextView>(); //  直接父类
List<? super Button> buttons = new ArrayList<Object>(); // 间接父类
  • Wildcard? Indicates that the generic type of List is an unknown type
  • super restricts the lower bound of this unknown type, that is, the generic type must satisfy the restriction conditions of this super. We often use super in class methods. The scope here includes not only the direct and indirect parent classes of Button, but also the lower bound Button itself. super also supports interface
summary

Java's generics do not support covariance and contravariance. Java's generic types will be type erased at compile time

  • Can use generic wildcard? extends to make generics support covariance
  • Can you use generic wildcards? super to make generics support contravariance

Guess you like

Origin blog.csdn.net/xufei5789651/article/details/102458176