Java-very deep I only know one-generics

Java-very deep I only know one-generics

Table of contents

generic history

Generic class/generic interface

 generic method

 Generic properties

 Generic constraints


  • generic history

  1. JAVA generics (generics) is a new feature introduced in JDK 5, which allows programmers to specify type parameters when programming, so that the compiler can detect illegal types when compiling code. The essence of generics is a parameterized type, which means that the type of data being manipulated is specified as a parameter.
  2. After JDK 7, generics can be abbreviated as ArrayList<String> list = new ArrayList<>();
  • Generic class/generic interface

  1. Generally, it is represented by T, E, K, V, N, R, and ? represents a wildcard/placeholder, convention and habit. Of course, it can be expressed in any way, as long as it conforms to the java specification.
    1. E - Element (used in collections, because collections store elements), enumeration Enum
    2. T - Type (Java class)
    3. K - Key
    4. V - Value
    5. N - Number (numeric type)
  2. In the following example, the class (interface) name is followed by a generic declaration (for example: <T> and <T1,T2,R>), which represents the number of generics in this class (interface), that is, the current class (Generic) How many data types can be used at the same time.
  3. If the attributes, method parameters, and method return values ​​of the same generic declaration appear in the class (interface), it means that they are of the same type.

 

  •  generic method

  1. Generics that have been declared in a generic class (interface) can be used directly in the generic method, and those that have not been declared need to be declared separately on the method.
  2. Repeated declarations on methods that have been declared in the class (interface), data domain issues.
  3. The following example uses generic parameters and generic return values.

  •  Generic properties

  1. To use generics for class attributes, they need to be declared in advance in the class (interface).
  2. To use generics for method attributes, they need to be declared in advance of the method.
  3. The generic type given by the GenericClazz class in the following example is Demo, of course it can be any type, and the reference class is used here as a demonstration.

  •  Generic constraints

  1. There are two reference data types Demo2 and GenericClazz in the following view, and Demo2 inherits GenericClazz.
  2. ? Indicates a placeholder/wildcard, indicating the current type.
  3. ArrayList<? extends GenericClazz>: Declare the upper bound, indicating that the parameterized type may be the specified GenericClazz type, or any subtype of this type.
  4. ArrayList<? super Demo2>: Declare the lower bound, indicating that the parameterized type may be the specified Demo2 type, or any parent type of this type.
  5. ? extends / ? super will lead to generic covariance, and the red error points in the example are all caused by extends generic covariance
    1. extends: The object is readable and not writable.
    2. super : The object is readable and writable.

 

Guess you like

Origin blog.csdn.net/scdncby/article/details/132054185