Java generic usage (classes, methods, interface protocols, type wildcards, wildcard upper and lower bounds)

1. Introduction

  • 泛型: JDK5is a feature introduced in , which provides a compile-time type safety detection mechanism, which allows illegal types to be detected at compile time. Its essence is a parameterized type, which means that the data type being manipulated is specified as a parameter .

  • When it comes to parameters, the most familiar is to define a method with formal parameters, and then pass the actual parameters when calling the method. So how do you understand parameterized types? As the name implies, it is to parameterize the type from the original specific type, and then pass in the specific type when using/calling.

  • This parameter type can be used in classes, methods, and interfaces, which are called generic classes, generic methods, and generic interfaces, respectively.

  • 泛型Definition format:

    • <类型>: Specifies the format of a type, where the type can be regarded as a formal parameter.

    • <类型1,类型2...>: Specify the format of multiple types, and separate multiple types with commas. The types here can be regarded as formal parameters.

    • In the future, the type given in the specific call can be regarded as an actual parameter, and the type of the actual parameter can only be a reference data type.

  • 泛型the benefits of:

    • Moved runtime problems to compile time

    • Avoid casts

2. Generic class

  • 泛型类The definition format of:

    • Format:修饰符 class 类名<类型> { }

    • example:public class Generic<T> { }

    • Here Tcan be written as any identifier, and common T、E、K、 Vparameters such as and are often used to represent generics.

  • Define a public generic classGeneric.java

    public class Generic<T> {
      // 定义私有属性
      private T t;
      // 重写 get 方法
      public T getT() {
        return t;
      }
      // 重写 set 方法
      public void setT(T t) {
        this.t = t;
      }
    }
    复制代码
  • mainWhen used in a function, specify the generic type as String, when using a generic class, all generic types point to this String.

    public class test {
      public static void main(String[] args) {
        // 新建对象
        Generic<String> g1 = new Generic<String>();
        // 使用 set 方法
        g1.setT("dzm");
      }
    }
    复制代码

    image.png

3. Generic method

  • 泛型方法The definition format of:

    • Format:修饰符 <类型> 返回值类型 方法名(类型量名){ }

    • example:public <T> void show(T t){ }

  • Case:

    public class test {
      public static void main(String[] args) {
        // 使用泛型方法
        DZMLog("dzm");
        DZMLog(88);
        DZMLog(true);
      }
    
      // 泛型方法
      static <T> void DZMLog(T t) {
        System.out.println(t);
      }
    }
    复制代码

    output:

    dzm
    88
    true
    复制代码

4. Generic Interface Protocol

  • 泛型接口协议The definition format of:

    • Format:修饰符 interface 接口名<类型>{ }

    • example:public interface Generic<T>{ }

  • define an Generic.javainterface protocol

    public interface Generic<T> {
      // 接口方法定义
      public default void show(T t) {
        System.out.println(t);
      }
    }
    复制代码
  • Define a GenericPro.javaclass to conform to the Generic.javainterface protocol

    public class GenericPro<T> implements Generic<T> {
      // 继承协议并实现协议方法
      @Override
      public void show(T t) {
        // 实现协议方法
      }
    }
    复制代码

Five, type wildcard

  • In order to represent Listthe , you can use类型通配符

  • 类型通配符The definition format of :

    • Type wildcard:<?>

    • List<?>: Indicates that the element type is unknown, Listand its elements can be matched 任何的类型.

    • This wildcard Listonly indicates that it is Listthe parent class of various generics, and cannot add elements to it.

  • 通配符的 上下限

    • 如果说我们不希望 List<?> 是任何泛型 List 的父类,只希望它代表某一类泛型 List 的父类,可以使用类型通配符的上限。

    • 类型通配符 - 上限<? extends 类型>

    • 范例 List<? extends Number>:它表示的类型是 Number或者其子类型一定不能是父类型

    • 类型通配符 - 下限<? super 类型>

    • 范例 List<? super Number>:它表示的类型是 Number或者其父类型一定不能是子类型

  • 案例:

    import java.util.List;
    import java.util.ArrayList;
    
    public class test {
      public static void main(String[] args) {
        // 类型通配符: <?>
        List<?> list1 = new ArrayList<String>();
        List<?> list2 = new ArrayList<Number>();
        List<?> list3 = new ArrayList<Integer>();
    
        // 类型通配符 上线: <? extends 类型>
        List<? extends Number> list5 = new ArrayList<String>(); // 这个会报错,因为 String 不属于 Number或者其子类型
        List<? extends Number> list6 = new ArrayList<Number>();
        List<? extends Number> list7 = new ArrayList<Integer>();
    
        // 类型通配符 下线: <? super 类型>
        List<? super Number> list5 = new ArrayList<String>(); // 这个会报错,因为 String 不属于 Number或者其父类型
        List<? super Number> list6 = new ArrayList<Number>();
        List<? super Number> list7 = new ArrayList<Integer>(); // 这个会报错,因为 Integer 不属于 Number或者其父类型
      }
    }
    复制代码

Guess you like

Origin juejin.im/post/7010954093325713445