[英]Instantiating a generic type
I'm trying to convert an existing class to use generics, and am getting stumped while converting the constructors. 我正在尝试将现有类转换为使用泛型,并且在转换构造函数时感到难过。
The original class was a POJO that contained logic for moving from one room to another in a text-based console game. 原始类是一个POJO,包含在基于文本的控制台游戏中从一个房间移动到另一个房间的逻辑。 Literally, it was a class that held some string triggers that would fire the action (eg. the user types "walk right"), a description, and a pointer to the new Location.
从字面上看,它是一个类,它包含一些可触发动作的字符串触发器(例如,用户键入“向右走”),描述和指向新位置的指针。
The current, non generic version of the class looks like this: 该类的当前非泛型版本如下所示:
public class Navigation implements Serializable {
private static final long serialVersionUID = 1L;
private String trigger;
private String description;
private Location target;
public Navigation() {
this("", "", new Location());
}
public Navigation(String trigger, String description, Location target) {
this.trigger = trigger;
this.description = description;
this.target = target;
}
// plus getters, setters, etc.
}
(The Location
class is another POJO that describes a location. It is irrelevant.) (
Location
类是另一个描述位置的POJO。它是无关紧要的。)
I want to extend the Navigation
class to be able to handle targets that are not Location
s. 我想扩展
Navigation
类,以便能够处理不是Location
的目标。 I thought that the best way to do this would be to convert the Navigation
class to use generics, so I tried this: 我认为最好的方法是将
Navigation
类转换为使用泛型,所以我尝试了这个:
public class Navigation<T> implements Serializable {
private static final long serialVersionUID = 2L;
private String trigger;
private String description;
private T target;
public Navigation() {
this("", "", new T());
}
public Navigation(String trigger, String description, T target) {
this.trigger = trigger;
this.description = description;
this.target = target;
}
// plus getters, setters, etc.
}
However, this doesn't compile at the line this("", "", new T());
但是,这不会在此行编译
this("", "", new T());
because T
cannot be instantiated. 因为
T
无法实例化。 Is is possible to instantiate the generic type object in this context? 是否可以在此上下文中实例化泛型类型对象?
You basically have two choices: 你基本上有两个选择:
1.Require an instance: 1.需要一个实例:
public Navigation(T t) {
this("", "", t);
}
2.Require a class instance: 2.获取一个类实例:
public Navigation(Class<T> c) {
this("", "", c.newInstance());
}
You could use a factory pattern, but ultimately you'll face this same issue, but just push it elsewhere in the code. 您可以使用工厂模式,但最终您将面临同样的问题,但只需将其推送到代码中的其他位置即可。
No, and the fact that you want to seems like a bad idea. 不,而且你想要的事实似乎是一个坏主意。 Do you really need a default constructor like this?
你真的需要像这样的默认构造函数吗?
You cannot do new T()
due to type erasure . 由于类型擦除,你不能做
new T()
。 The default constructor can only be 默认构造函数只能是
public Navigation() {
this("", "", null);
}
You can create other constructors to provide default values for trigger and description. 您可以创建其他构造函数以提供触发器和描述的默认值。 You need an concrete object of
T
. 你需要一个
T
的具体对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.