我有一个枚举,它实现了一个接口。该接口的目的仅是在多个枚举之间创建一个边界,以便以后可以实现插件系统。 (This post可能会稍微清除一下。)
我的库枚举类似:
public interface Resource extends Displayable<Resource> {
// all the methods that implements my enum
// Displayable is just an interface that I need in my game
// Just doesn't consider it, it can't interfer
}
以及实现此接口的枚举的一个示例:
public enum LibraryEnum implements Resource {
// static final fields
// fields and resources
// all implemented methods
}
事实是,我想将其存储为接口,以实现某种枚举继承,因为我将要创建一个插件系统。任何开发人员仅需实现此唯一接口即可在游戏中添加资源。但是我不知道他(或她)会如何命名(可能会有一些双打)。
我有一个带有相关字段的类,如下所示:
@DatabaseTable(tableName = "packs")
public class Pack implements Displayable<Pack> {
@DatabaseField(columnName = "id", generatedId = true)
private Long packId;
// Here it is
@DatabaseField
private Resource resource;
// Is there any annotation arguments to add ?
@DatabaseField
private int quantity;
// Some other fiels
// Then constructors and methods
}
我已经阅读了一些文档,并说要创建DataPersister接口的自定义实现。因此,我开始这样做,但是要实现的方法太多(20个?),我不知道从哪里开始和从哪里结束。也许这是错误的方法?该文档显示了一个示例,这些示例中甚至没有方法在哪里,以及Java中已经存在的类型(日期和日期时间)。
我该如何实现?还是有可能吗?如果不是,无论如何都要做我想要做的事情(在数据库中存储未知的const枚举字段)?
我已经阅读了一些文档,并说要创建DataPersister接口的自定义实现。所以我开始做,但是要实现的方法太多(20个?),我不知道从哪里开始和从哪里结束。
首先,您应该考虑RTFM on custom persisters。我在ORMLite文档上花了很多时间。
正确的做法是从ORMLite扩展当前实现的持久性。例如,如果要保留枚举,则可以扩展BaseEnumType
。如果没有任何持久器起作用,则应扩展通用BaseEnumType
。
使用BaseDataType
,您需要实现的是:
BaseDataType
尽管不是必需的,但您可能还想覆盖:
BaseDataType
通常,您需要重写其他两种方法来调整自定义持久性的行为。