我有了一个枚举属性的实体:使用枚举在开关/箱
// MyFile.java
public class MyFile {
private DownloadStatus downloadStatus;
// other properties, setters and getters
}
// DownloadStatus.java
public enum DownloadStatus {
NOT_DOWNLOADED(1),
DOWNLOAD_IN_PROGRESS(2),
DOWNLOADED(3);
private int value;
private DownloadStatus(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
我要保存在数据库中这个实体和检索。问题是我保存在数据库中的int值,我得到int值!我不能使用如下的开关:
MyFile file = new MyFile();
int downloadStatus = ...
switch(downloadStatus) {
case NOT_DOWNLOADED:
file.setDownloadStatus(NOT_DOWNLOADED);
break;
// ...
}
我该怎么办?
可能是多数民众赞成在最坏的情况下,但唯一的解决办法是我可以使用一个不同的getter通过扩展类,返回值作为枚举。 –