[英]Can a field of a Java class be of multiple types without using generics?
I have a class, which's instances will be serialized. 我有一个类,其实例将被序列化。 Thus the class implements
Serializable
. 因此,该类实现了
Serializable
。 The class has a field of type List
, which must be Serializable
as well of course. 该类有一个
List
类型的字段,当然也必须是Serializable
。 Imagine the class looking something like this: 想象一下这个类看起来像这样:
import java.io.Serializable;
import java.util.List;
public class Report implements Serializable {
private static final long serialVersionUID = 12345L;
private List<Record> records = new ArrayList<>();
}
Now I know that ArrayList
is Serializable
, but I'd rather declare my field to be of some interface type. 现在我知道
ArrayList
是Serializable
,但我宁愿声明我的字段是某种接口类型。
The issue comes when our SonarQube analyzes the code. 我们的SonarQube分析代码时会出现问题。 It complains, that the field
records
should be of some Serializable
type as well and SonarQube is right. 它抱怨说,现场
records
应该是一些Serializable
类型,而SonarQube是正确的。
But due to the fact that the field is an implementation detail (a private member), I don't want to use generics on the class. 但是由于该字段是一个实现细节(私有成员),我不想在类上使用泛型。
And here comes my question: With the self applied constraints to declare the field of some interface type and not use generics, is it possible to declare the field to be of multiple (interface) types? 这里有我的问题:使用自我应用约束来声明某些接口类型的字段而不使用泛型,是否可以将字段声明为多个(接口)类型?
FYI: I know one can cast to multiple types, but that's something entirely different. 仅供参考:我知道可以投射到多种类型,但这是完全不同的。
Object o = (List<Serializable> & Serializable) new ArrayList<Serializable>();
The above is perfectly legal although unnecessary. 尽管不必要,上述内容完全合法。
It seems like your field is private
anyway, so whether it's a List
or an ArrayList
does not really matter. 看起来你的字段无论如何都是
private
的,所以无论是List
还是ArrayList
并不重要。 You could just make the field a serializable ArrayList
and use List
in the getter. 您可以将该字段
ArrayList
可序列化的ArrayList
并在getter中使用List
。
public class Report implements Serializable {
private ArrayList<Record> records = new ArrayList<>();
...
public List<Record> getRecords() {
return this.records;
}
}
(While this does not answer the question of whether a field can have multiple types, it might be a solution to your actual problem.) (虽然这不能回答一个字段是否可以有多种类型的问题,但它可能是您实际问题的解决方案。)
The short and disillusioning answer is: No, a field cannot be of multiple types simultaneously without using generics or custom classes. 简短且幻想破灭的答案是:不,如果不使用泛型或自定义类,字段不能同时具有多种类型。
What I was looking for was a way to declare a field - actually any kind of variable - to be of an intersection type. 我正在寻找的是一种将一个字段 - 实际上是任何一种变量 - 声明为交集类型的方法。 Whilst intersection types exist in Java since the introduction of generics in Java 6, lambda support in Java 8 needed and brought us intersection casts.
虽然自Java 6中引入泛型以来Java中存在交集类型 ,但Java 8中的lambda支持需要并为我们带来交叉转换。 Still, intersection types for variables have yet to be implemented.
但是,尚未实现变量的交集类型。 Let's hope for the best.
让我们期盼最好的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.