Container [generic class, generic interface, generic method, generic method and variable parameters] (1) - comprehensive detailed explanation (learning summary --- from entry to deepening)

Table of contents

 Generics

generic class

 generic interface

generic method 

Generic methods and variable parameters 


 Generics

 In order to better learn containers, we must first learn a concept: generics.
The basic concept of generics, generics is a new feature added after JDK5.0.
The essence of generics is "parameterization of data types". The data types processed are not fixed, but can be passed in as parameters. We can understand "generics" as a placeholder for data types (similar to: formal parameters), which tells the compiler that the actual type must be passed in when calling the generic.

Parameterized type, in plain English, means:
  1. Pass the type as a parameter.
2. <data type> can only be a reference type.

The benefits of generics

Without using generics, we can use the Object type to implement any parameter type , but we need to force type conversion when using it. This requires programmers to clearly know the actual type, otherwise it may cause type conversion errors; however, we cannot identify such errors during compilation, and can only find such errors during runtime . The advantage of using generics is that this kind of error can be identified at compile time , which has better security ; at the same time, all type conversions are completed by the compiler, which is automatically converted from the programmer's point of view, which improves code security. readability.

To sum up, there are two main benefits of using generics:
1. The code is more readable [no mandatory conversion]
2. The program is safer [as long as there is no warning during compilation, there will be no ClassCastException during runtime]

 type erasure

The type parameters written by generics are used in coding, and the compiler will remove them at compile time, which is called "type erasure".
Generics are mainly used in the compilation stage . The bytecode class file generated after compilation does not contain the type information in generics, and the type conversion involved is still an ordinary mandatory type conversion. The type parameter will be replaced with Object after compilation, and the virtual machine does not know the generic type at runtime.
Generics are mainly to facilitate programmers' code writing and better security detection.

 Real-time effect feedback
1. In which version of JDK is the generic type added?
A JDK5.0
B JDK6.0
C JDK7.0
D JDK8.0

2. What is the essence of generics?
A Parameterization of data
B Parameterization of objects
C Parameterization of data types
D Parameterization of basic types

Answer
1=>A 2=>C

generic class

 generic tag


When defining a generic type, several tags are generally used: E, T, K, V, N, ? . They agree on
the meaning of the common name as follows:

generic tag  corresponding word  illustrate
E Element Used in containers to represent the elements in the container
T Type Represents an ordinary JAVA class
K Key Represents a key, for example: key Key in Map
V Value Indicates the value
N Number Represents a numeric type
?   Represents an indeterminate JAVA type

Use of generic classes

 Grammatical structures

public class 类名<泛型标识符号> {  }

public class 类名<泛型标识符号,泛型标识符号> {  }

 example

public class Generic<T> {
  private T  flag;
  public void setFlag(T flag){
    this.flag = flag;
 }
  public T getFlag(){
    return this.flag;
 }
}
public class Test {
  public static void main(String[] args) {

    //创建对象时,指定泛型具体类型。
    Generic<String> generic = new Generic<>();

    generic.setFlag("admin");

    String flag = generic.getFlag();
    System.out.println(flag);
   
    //创建对象时,指定泛型具体类型。
    Generic<Integer> generic1 = new Generic<>();

    generic1.setFlag(100);

    Integer flag1 = generic1.getFlag();
    System.out.println(flag1);
 }
}

Real-time effect feedback

1. Which of the following options is the correct syntax for defining a generic class
A public class <generic identifier> class name
B public <generic identifier> class class name
C <generic identifier> public class class name
D public class class name <generic identifier>

Answer
1 => D

 generic interface

 Generic interfaces are declared in the same way as generic classes.

Use of generic interfaces

Grammatical structures

public interface 接口名<泛型标识符号> {   }
public interface 接口名<泛型标识符号,泛型标识符号>{   }

 example

public interface IGeneric<T> {
  T getName(T name);
}



//在实现接口时传递具体数据类型
public class IgenericImpl implements
Igeneric<String> {
  @Override
  public String getName(String name) {
    return name;
 }
}


//在实现接口时仍然使用泛型作为数据类型
public class IGenericImpl2<T> implements
IGeneric<T>{
  @Override
  public T getName(T name) {
    return name;
 }
}

public class Test {
  public static void main(String[] args) {
    IGeneric<String> igeneric= new  IGenericImpl();
    String name = igeneric.getName("old");
    System.out.println(name);
    IGeneric<String> igeneric1 = new IGenericImpl2<>();
    String name1 = igeneric1.getName("it");
    System.out.println(name1);
 }
}

Real-time effect feedback
1. Which of the following options is the correct syntax for defining a generic
interface A public interface<generic identifier> interface name
B public <generic identifier> interface interface name
C <generic identifier> public interface interface name
D public interface interface name <generic identifier>

Answer
1 => D

generic method 

 Generics defined on classes can also be used in methods. However, we often need to use generics only on a certain method, at this time we can use generic methods.
When calling a generic method, you don't need to tell the compiler what type it is like a generic class, the compiler can automatically infer the type

 Use of generic methods


 non-static method

Non-static methods can use the generics defined in the generic class, or the generics can be defined on the method.

 Grammatical structures

//无返回值方法
public <泛型标识符号> void getName(泛型标识符号name) {  }

//有返回值方法
public <泛型标识符号> 泛型标识符号 getName(泛型标识符号 name) {  }

 example

public class MethodGeneric {
  public <T> void setName(T name){
    System.out.println(name);
 }
  public <T> T getAge(T age){
    return age;
 }
}
public class Test2 {
  public static void main(String[] args) {

    MethodGeneric methodGeneric = new MethodGeneric();

    methodGeneric.setName("old");

    Integer age = methodGeneric.getAge(123);

    System.out.println(age);
 }

static method

One thing to note when using generics in static methods is that static methods cannot access the generics defined on the class, so the generics must be defined on the method.

Grammatical structures

//无返回值静态方法
public static <泛型标识符号> void setName(泛型标识符号 name){  }

//有返回值静态方法
public static <泛型标识符号> 泛型表示符号getName(泛型标识符号 name){  }

example

public class MethodGeneric {
  public static <T> void setFlag(T flag){
    System.out.println(flag);
 }
  public static <T> T getFlag(T flag){
    return flag;
 }
}
public class Test4 {
  public static void main(String[] args) {

    MethodGeneric.setFlag("old");

    Integer flag1 =  MethodGeneric.getFlag(123123);

    System.out.println(flag1);
 }
}

Real-time effect feedback
1. Which of the following options is the correct syntax for defining generic methods
A <generic identifier> public void getName(generic identifier name)
B public void <generic identifier> getName(generic identifier name)
C public <generic identifier> void getName(generic identifier name)
D public void getName <generic identifier>(generic identifier name)

Answer
1 => C

Generic methods and variable parameters 

 In generic methods, generics can also define variadic types.

Grammatical structures

public <泛型标识符号> void showMsg(泛型标识符号... agrs){  }

example

public class MethodGeneric {
  public <T> void method(T...args){

    for(T t:args){
      System.out.println(t);
     }

   }
}

public class Test5 {
  public static void main(String[] args) {
    MethodGeneric methodGeneric = new  MethodGeneric();

    String[] arr = new String[]{"a","b","c"};

    Integer[] arr2 = new Integer[]{1,2,3};

    methodGeneric.method(arr);

    methodGeneric.method(arr2);
  }
}

Real-time effect feedback
1. Which of the following options is correct Use generics in variable parameters
A public <generic identifier> void showMsg(generic identifier... agrs)
B public void showMsg(<generic identifier> ... agrs)
C public <generic identifier> void showMsg(<generic identifier>... agrs)

D public <generic identifier> void showMsg(Object... agrs)


Answer
1 => A

Guess you like

Origin blog.csdn.net/m0_58719994/article/details/131669512