In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what are the Java generic constructors". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Overview
We discussed the basics of Java Generics before. In this article, we will learn about common constructors in Java. A generic constructor is a constructor that requires at least one generic type parameter. We will see that generic constructors do not always appear in generic classes, and not all constructors in generic classes have to be generic.
two。 Non-generic class
First, write a simple class: Entry, which is not a generic class:
Public class Entry {private String data;private int rank;}
In this class, we will add two constructors: a basic constructor with two parameters and a generic constructor.
2.1 basic constructor
Entry's first constructor: a simple constructor with two parameters:
Public Entry (String data, int rank) {this.data = data;this.rank = rank;}
Now, let's use this basic constructor to create an Entry object
Testpublic 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 constructor is the generic constructor
Public Entry (E element) {this.data = element.toString (); this.rank = element.getRank ();}
Although the Entry class is not generic, it has a generic constructor with an argument of E.
Generic type E is restricted and should implement the Rankable and Serializable interfaces.
Now, let's take a look at the Rankable interface. Here is one of the methods:
Public interface Rankable {public int getRank ();}
Suppose we have a class that implements the Rankable 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;} @ Overridepublic int getRank () {return sales;}}
Then we can create the Entry object using the generic constructor and Product:
@ Testpublic 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, let's look at the generic class: GenericEntry
Public class GenericEntry {private T data;private int rank;}
We will add the same two types of constructors to this class as in the previous section.
3.1 basic constructor
First, let's write a simple non-generic constructor for the GenericEntry class:
Public GenericEntry (int rank) {this.rank = rank;}
Although GenericEntry is a generic class, this is a simple constructor with no arguments. We can now use this constructor to create the GenericEntry:
Testpublic void givenNonGenericConstructor_whenCreateGenericEntry_thenOK () {GenericEntry entry = new GenericEntry (1); assertNull (entry.getData ()); assertEquals (1, entry.getRank ());}
3.2 generic constructor
Next, add a second constructor to the class:
Public GenericEntry (T data, int rank) {this.data = data;this.rank = rank;}
This is a generic constructor that has a data parameter of the generic type T. Note that we do not need to add it to the constructor declaration because it is implicit. Now, let's test the generic constructor:
Testpublic void givenGenericConstructor_whenCreateGenericEntry_thenOK () {GenericEntry entry = new GenericEntry ("sample", 1); assertEquals ("sample", entry.getData ()); assertEquals (1, entry.getRank ());}
4. Different types of generic constructors
In a generic class, there is also a constructor whose generic type is different from the generic type of the class:
Public GenericEntry (E element) {this.data = (T) element;this.rank = element.getRank ();}
The GenericEntry constructor has an argument of type E, which is different from the T type. Let's see how it actually works:
@ Testpublic void givenGenericConstructorWithDifferentType_whenCreateGenericEntry_thenOK () {Product product = new Product ("milk", 2.5); product.setSales (30); GenericEntry entry = new GenericEntry (product); assertEquals (product, entry.getData ()); assertEquals (30, entry.getRank ());}
Note: in the example, we use Product (E) to create a GenericEntry of type Serializable (T), and we can use this constructor only if the parameters of type E can be converted to T.
5. Multiple pan-types
Next, we have two generic classes, MapEntry, with generic type parameters:
Public class MapEntry {private K key;private V value;public MapEntry (K key, V value) {this.key = key;this.value = value;}}
MapEntry has a generic constructor with two parameters, each of which is of a different type. Let's test it with a simple unit test:
@ Testpublic void givenGenericConstructor_whenCreateGenericEntryWithTwoTypes_thenOK () {MapEntry entry = new MapEntry ("sample", 1); assertEquals ("sample", entry.getKey ()); assertEquals (1, entry.getValue (). IntValue ());}
6. Wildcard character
Finally, we can use wildcards in generic constructors:
Public GenericEntry (Optional
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.