03. Generics

Catalog introduction

  • 1. Overview of Generics
  • 2. Generic format
  • 3. The benefits of generics
  • 4. The use of generics
    • 4.1 Overview and use of generic classes
    • 4.2 Overview and use of generic methods
    • 4.3 Overview and Use of Generic Interfaces
  • 5. Generic advanced wildcards
  • 6. Overview and use of variable parameters

1. Overview of Generics

  • 1.1 Origin of generics

    • Our collection can store elements of multiple data types, so there is no problem in storage, but when getting elements and downcasting, there may be an error, and this error is ClassCastException . Obviously, the collection's This feature of elements that can store multiple data types is not very friendly, and the program has some security risks, so in order to find out such security risks, we should limit the data type of a set to store elements, and we only let him store in the unified If the data type is an element, then there will be no such security risk when doing downcasting. How can I limit the collection to only store elements of the same data type? Generics need to be used.
  • 1.2 Basic overview

    • A special type that defers type-specific work until object creation or method invocation. Parameterized types pass the type as a parameter.

2. Generic format

  • 2.1 The generic format is as follows
    • <data type> The data type here can only be a reference data type
    • <DataType1 , DataType2 , ....>

3. The benefits of generics

  • 3.1 Benefits
    • (1): Advance the runtime problem to the compilation time
    • (2): Avoid forced type conversion
    • (3): Optimized the program design and solved the yellow warning line

4. The use of generics

  • 4.1 Overview and use of generic classes

    • A: Generic class overview: define generics on classes
    • B: Definition format: public class class name <generic type 1,…>
    • C: Note: Generic types must be reference types
  • 4.2 Overview and use of generic methods

    • A: Overview of generic methods: define generics on methods
    • B: Definition format: public <generic type> return type method name (generic type variable name)
public <T> void show(T t) {

}
  • 4.3 Overview and Use of Generic Interfaces
    • A: Overview of generic interfaces: define generics on interfaces
    • B: Definition format: public interface interface name <generic type>
/**
 * 泛型接口的定义格式:        修饰符  interface 接口名<数据类型> {}
 */
public interface Inter<T> {
    public abstract void show(T t) ;
}

/**
 * 子类是泛型类
 */
public class InterImpl<E> implements Inter<E> {
    @Override
    public void show(E t) {
        System.out.println(t);
    }
}


Inter<String> inter = new InterImpl<String>() ;
inter.show("hello") ;

5. Generic advanced wildcards

  • 5.1 Advanced usage techniques
    • A: Generic wildcard <?>: Any type, if not specified, then it is Object and any Java class
    • B:? extends E: down-limited, E and its subclasses
    • C:? super E: Up-qualified, E and its superclasses

6. Overview and use of variable parameters

  • 6.1 Overview
    • A: Overview of variable parameters: When defining a method, I don't know how many parameters to define
    • B: format: modifier return value type method name (data type... variable name) {}
    • C: Notes:
      • a: The variable here is actually an array
      • b: If a method has variable parameters and there are multiple parameters, then the variable parameter must be the last one

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325025850&siteId=291194637