1 Overview
Before we discussed Java Generics
the basics. In this article, we will learn general constructor in Java. Generic constructor constructor will need at least a generic type parameter. We will see a generic constructor does not appear in all generic classes, but not all of the generic class constructor must be generic.
2. Non-generic class
First, let's write a simple class: Entry, it's not generic classes:
public class Entry {
private String data; private int rank; } 复制代码
In this class, we add two constructors: one with the basic configuration and function of the two parameters a common constructors.
2.1 Basic constructor
Entry
The first constructor: simple constructor takes two parameters:
public Entry(String data, int rank) { this.data = data; this.rank = rank; } 复制代码
Now, let's use this function to create a basic structure of Entry
an object
@Test
public void givenNonGenericConstructor_whenCreateNonGenericEntry_thenOK() { Entry entry = new Entry("sample", 1); assertEquals("sample", entry.getData()); assertEquals(1, entry.getRank()); } 复制代码
2.2 Generic constructor
Next, the second configuration is a generic Constructor:
public <E extends Rankable & Serializable> Entry(E element) {
this.data = element.toString();
this.rank = element.getRank();
}
复制代码
Although the Entry
class is not common, but it has a parameter E
generic constructor.
Generic type E
is restricted, it should implement Rankable
and Serializable
interface.
Now, let's look at Rankable
the interface, the following is one way:
public interface Rankable {
public int getRank(); } 复制代码
Suppose we have an implementation Rankable
class interface -Product
public class Product implements Rankable, Serializable { private String name; private double price; private int sales; public Product(String name, double price) { this.name = name; this.price = price; } @Override public int getRank() { return sales; } } 复制代码
Then we can use the generic constructors and Product
create Entry
objects:
@Test
public void givenGenericConstructor_whenCreateNonGenericEntry_thenOK() { Product product = new Product("milk", 2.5); product.setSales(30); Entry entry = new Entry(product); assertEquals(product.toString(), entry.getData()); assertEquals(30, entry.getRank()); } 复制代码
3. generic class
Next, we look at the generic class:GenericEntry
public class GenericEntry<T> { private T data; private int rank; } 复制代码
We will add one with the same two types of constructors in this class.
3.1 basis constructor
First, let us for the GenericEntry
class to write a simple non-generic constructors:
public GenericEntry(int rank) { this.rank = rank; } 复制代码
Although GenericEntry
a generic class, but this is a simple, no-argument constructor.
Now, we can use this constructor to create GenericEntry
:
@Test
public void givenNonGenericConstructor_whenCreateGenericEntry_thenOK() { GenericEntry<String> entry = new GenericEntry<String>(1); assertNull(entry.getData()); assertEquals(1, entry.getRank()); } 复制代码
3.2 Generic constructor
Next, add a second constructor class:
public GenericEntry(T data, int rank) { this.data = data; this.rank = rank; } 复制代码
This is a generic constructor, which has a generic parameter data type T. Note that we do not need to add in the constructor statement, because it was implicit.
Now, let's test the general constructor:
@Test
public void givenGenericConstructor_whenCreateGenericEntry_thenOK() { GenericEntry<String> entry = new GenericEntry<String>("sample", 1); assertEquals("sample", entry.getData()); assertEquals(1, entry.getRank()); } 复制代码
4 different types of generic constructor
In generic classes, there is a constructor, which is a generic type of generic type different categories:
public <E extends Rankable & Serializable> GenericEntry(E element) {
this.data = (T) element;
this.rank = element.getRank();
}
复制代码
GenericEntry
Type constructor has E
a parameter that the T
different types. Let's take a look at its practical effects:
@Test
public void givenGenericConstructorWithDifferentType_whenCreateGenericEntry_thenOK() { Product product = new Product("milk", 2.5); product.setSales(30); GenericEntry<Serializable> entry = new GenericEntry<Serializable>(product); assertEquals(product, entry.getData()); assertEquals(30, entry.getRank()); } 复制代码
Note: In the example, we used Product(E)
to create Serializable(T)
the type GenericEntry
, only when the type E
of argument can be converted T
, we can use this constructor.
The various types of pan
Next, we have two generic classes of generic type parameters MapEntry
:
public class MapEntry<K, V> { private K key; private V value; public MapEntry(K key, V value) { this.key = key; this.value = value; } } 复制代码
MapEntry
There is a generic constructor two parameters, each parameter is different types. Let's use a simple unit test test:
@Test
public void givenGenericConstructor_whenCreateGenericEntryWithTwoTypes_thenOK() { MapEntry<String,Integer> entry = new MapEntry<String,Integer>("sample", 1); assertEquals("sample", entry.getKey()); assertEquals(1, entry.getValue().intValue()); } 复制代码
6. Tsuhaifu
Finally, we can use wildcards in the generic constructor:
public GenericEntry(Optional<? extends Rankable> optional) {
if (optional.isPresent()) { this.data = (T) optional.get(); this.rank = optional.get().getRank(); } } 复制代码
Here, we have GenericEntry
to bind using wildcards constructor Optional
type:
@Test
public void givenGenericConstructorWithWildCard_whenCreateGenericEntry_thenOK() { Product product = new Product("milk", 2.5); product.setSales(30); Optional<Product> optional = Optional.of(product); GenericEntry<Serializable> entry = new GenericEntry<Serializable>(optional); assertEquals(product, entry.getData()); assertEquals(30, entry.getRank()); } 复制代码
Please note that we should be able to type an optional parameter (Product example) converted to GenericEntry
type (Serializable example).
7. Conclusion
In this article, we learned how generic and non-generic classes define and use generic constructor.
The complete source code can be GitHub
acquired (Click to view the original text).
Original link: www.baeldung.com/java-generi...
Author: baeldung
Translator: Emma
Recommended public concern number: Gangster outside the pot
Daily push foreign outstanding technical translation of the article, inspirational domestic help developers better growth!