Spring Boot实现STOMP协议的WebSocket的方法步骤
作者:liululee 时间:2022-10-01 07:12:27
1.概述
我们之前讨论过Java Generics的基础知识。在本文中,我们将了解Java中的通用构造函数。 泛型构造函数是至少需要有一个泛型类型参数的构造函数。我们将看到泛型构造函数并不都是在泛型类中出现的,而且并非所有泛型类中的构造函数都必须是泛型。
2.非泛型类
首先,先写一个简单的类:Entry,它不是泛型类:
public class Entry {
private String data;
private int rank;
}
在这个类中,我们将添加两个构造函数:一个带有两个参数的基本构造函数和一个通用构造函数。
2.1 基本构造器
Entry第一个构造函数:带有两个参数的简单构造函数:
public Entry(String data, int rank) {
this.data = data;
this.rank = rank;
}
现在,让我们使用这个基本构造函数来创建一个Entry对象
@Test
public void givenNonGenericConstructor_whenCreateNonGenericEntry_thenOK() {
Entry entry = new Entry("sample", 1);
assertEquals("sample", entry.getData());
assertEquals(1, entry.getRank());
}
2.2 泛型构造器
接下来,第二个构造器是泛型构造器:
public <E extends Rankable & Serializable> Entry(E element) {
this.data = element.toString();
this.rank = element.getRank();
}
虽然Entry类不是通用的,但它有一个参数为E的泛型构造函数。
泛型类型E是受限制的,应该实现Rankable和Serializable接口。
现在,让我们看看Rankable接口,下面是其中一个方法:
public interface Rankable {
public int getRank();
}
假设我们有一个实现Rankable接口的类——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;
}
}
然后我们可以使用泛型构造函数和Product创建Entry对象:
@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.泛型类
接下来,我们看一下泛型类:GenericEntry
public class GenericEntry<T> {
private T data;
private int rank;
}
我们将在此类中添加与上一节相同的两种类型的构造函数。
3.1 基础构造器
首先,让我们为GenericEntry类编写一个简单的非泛型构造函数:
public GenericEntry(int rank) {
this.rank = rank;
}
尽管GenericEntry是泛型类,但这是一个简单的,没有任何参数的构造函数。
现在,我们可以使用此构造函数来创建GenericEntry:
@Test
public void givenNonGenericConstructor_whenCreateGenericEntry_thenOK() {
GenericEntry<String> entry = new GenericEntry<String>(1);
assertNull(entry.getData());
assertEquals(1, entry.getRank());
}
3.2 泛型构造器
接下来,在类中添加第二个构造函数:
public GenericEntry(T data, int rank) {
this.data = data;
this.rank = rank;
}
这是一个泛型构造函数,它有一个泛型类型T的数据参数。注意,我们不需要在构造函数声明中添加,因为它是隐含的。
现在,让我们测试一下通用构造函数:
@Test
public void givenGenericConstructor_whenCreateGenericEntry_thenOK() {
GenericEntry<String> entry = new GenericEntry<String>("sample", 1);
assertEquals("sample", entry.getData());
assertEquals(1, entry.getRank());
}
4.不同类型的泛型构造函数
在泛型类中,还有一个构造函数,其泛型类型与类的泛型类型不同:
public <E extends Rankable & Serializable> GenericEntry(E element) {
this.data = (T) element;
this.rank = element.getRank();
}
GenericEntry构造函数有类型为E的参数,该参数与T类型不同。让我们看看它的实际效果:
@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());
}
注意:在示例中,我们使用Product(E)创建Serializable(T)类型的GenericEntry,只有当类型E的参数可以转换为T时,我们才能使用此构造函数。
5.多种泛类型
接下来,我们有两个泛型类型参数的泛型类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有一个两个参数的泛型构造函数,每个参数都是不同的类型。让我们用一个简单的单元测试测试一下:
@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.通配符
最后,我们可以在泛型构造函数中使用通配符:
public GenericEntry(Optional<? extends Rankable> optional) {
if (optional.isPresent()) {
this.data = (T) optional.get();
this.rank = optional.get().getRank();
}
}
在这儿,我们在GenericEntry构造函数中使用通配符来绑定Optional类型:
@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());
}
请注意,我们应该能够将可选参数类型(Product示例)转换为GenericEntry类型(Serializable示例)。
7.结束语
在本文中,我们学习了如何在泛型和非泛型类中定义和使用泛型构造函数。
原文链接:https://www.baeldung.com/java-generic-constructors
来源:https://www.baeldung.com/java-generic-constructors
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java实现为Word每一页设置不同图片水印的效果
![](https://img.aspxhome.com/file/2023/0/85830_0s.jpg)
android图片处理之让图片变成圆形
![](https://img.aspxhome.com/file/2023/5/117115_0s.jpg)
使用Jackson反序列化遇到的问题及解决
C#设计模式之Singleton模式
![](https://img.aspxhome.com/file/2023/5/86995_0s.png)
Android实现在子线程中更新Activity中UI的方法
Android实现简单卡片布局
![](https://img.aspxhome.com/file/2023/0/87310_0s.png)
Android仿简书搜索框效果的示例代码
![](https://img.aspxhome.com/file/2023/7/83527_0s.gif)
C#中WPF颜色对话框控件的实现
![](https://img.aspxhome.com/file/2023/7/88437_0s.png)
Android自定义钟表特效
![](https://img.aspxhome.com/file/2023/9/110699_0s.jpg)
Java中为什么start方法不能重复调用而run方法可以?
![](https://img.aspxhome.com/file/2023/8/59598_0s.webp)
C#中WPF ListView绑定数据的实例详解
![](https://img.aspxhome.com/file/2023/8/104278_0s.png)
C# using的本质及使用详解
![](https://img.aspxhome.com/file/2023/0/76390_0s.png)
Java日常练习题,每天进步一点点(40)
![](https://img.aspxhome.com/file/2023/7/95977_0s.png)
AndroidStudio项目打包成jar的简单方法
![](https://img.aspxhome.com/file/2023/0/96810_0s.png)
利用java开发简易版扫雷游戏
![](https://img.aspxhome.com/file/2023/9/63489_0s.png)
C#中字符串编码处理
![](https://img.aspxhome.com/file/2023/2/77232_0s.jpg)
Android.bp语法和使用方法讲解
![](https://img.aspxhome.com/file/2023/7/111717_0s.png)
通过实例学习JAVA对象转成XML输出
![](https://img.aspxhome.com/file/2023/8/89478_0s.jpg)
在Java内存模型中测试并发程序代码
![](https://img.aspxhome.com/file/2023/7/59967_0s.png)
Java日期操作方法工具类实例【包含日期比较大小,相加减,判断,验证,获取年份等】
![](https://img.aspxhome.com/file/2023/9/59989_0s.jpg)