Java generics are used in constructors

In Java, we can use generics to define classes and methods to improve code reusability and type safety. In addition to using generics in classes and methods, we can also use generics in constructors to create objects. In this blog, we will focus on the usage and sample code of Java generics for construction methods.

Use generic constructors

Constructors in Java are special methods used to create objects. In the constructor, we can use generics to create objects with type safety. For example, the following code defines a generic class Boxthat can store objects of any type:

public class Box<T> {
    
    
  private T object;

  public Box(T object) {
    
    
    this.object = object;
  }

  public T getObject() {
    
    
    return object;
  }
}

In the code above, Boxthe class uses a type parameter Tto indicate that it can store objects of any type. In the constructor, we can use type parameters Tto create objects with type safety. For example, the following code creates an Boxobject that stores an Stringobject:

Box<String> box = new Box<>("Hello");

In the above code, <String>denote the type parameter, which will be replaced with the actual type String. Therefore, boxwhat is stored in an object is an Stringobject.

generic constructor

In addition to using generics in classes, you can also use generics in constructors. The main reason for using generic constructors is to create objects with type safety. For example, the following code defines a generic class Pairthat can store two objects of any type:

public class Pair<T, U> {
    
    
  private T first;
  private U second;

  public <V extends T, W extends U> Pair(V first, W second) {
    
    
    this.first = first;
    this.second = second;
  }

  public T getFirst() {
    
    
    return first;
  }

  public U getSecond() {
    
    
    return second;
  }
}

In the code above, Pairthe class uses the type parameters Tand Uto indicate that it can store two objects of any type. In the construction method, we use the generic type parameter Vand Wto represent the actual type parameter. In this constructor, we can use the type parameters Vand Wto create an object with type safety.

For example, the following code creates an Pairobject that stores an Stringobject and an Integerobject:

Pair<String, Integer> pair = new Pair<>("Hello", 123);

In the above code, <String, Integer>denoting the type parameter, it will be replaced with the actual type Stringand Integer.

sample code

The following is a simple Java program to demonstrate the use of Java generics for construction methods:

public class GenericsDemo {
    
    
  public static void main(String[] args) {
    
    
    Box<String> box = new Box<>("Hello");
    System.out.println(box.getObject());

    Pair<String, Integer> pair = new Pair<>("Hello", 123);
    System.out.println(pair.getFirst());
    System.out.println(pair.getSecond());
  }
}

class Box<T> {
    
    
  private T object;

  public Box(T object) {
    
    
    this.object = object;
  }

  public T getObject() {
    
    
    return object;
  }
}

class Pair<T, U> {
    
    
  private T first;
  private U second;

  public <V extends T, W extends U> Pair(V first, W second) {
    
    
    this.first = first;
    this.second = second;
  }

  public T getFirst() {
    
    
    return first;
  }

  public U getSecond() {
    
    
    return second;
  }
}

In this example, we define a generic class Boxthat can store objects of any type, and a generic class Pairthat can store two objects of any type. We use generics in Boxclasses and Pairclass constructors to create objects with type safety. In mainthe method, we create an Boxobject and an Pairobject and access the stored object by calling their methods.

Summarize

In Java, we can use generics to define classes, methods, and constructors to improve code reusability and type safety. Use generic constructors to create objects with type safety, avoiding the risk of type conversion exceptions at runtime. When writing Java programs, we should use generics as much as possible to improve the readability and maintainability of the code.

Guess you like

Origin blog.csdn.net/hj1993/article/details/131713322