【发布时间】:2016-08-31 23:43:23
【问题描述】:
我需要能够从 SQLite 数据库存储和检索枚举,我已经通过结合使用包装类和枚举来解决这个问题,但我觉得它的设计非常糟糕。
每次我想在枚举列表中添加一个新项目时,我都必须在三个不同的地方添加它。
枚举必须包含:
- 一个int(或字符串)来表示它
- 一个文本字符串,提供有关枚举本身的更多文本
- 一种从数据库中恢复其状态的方法
目前是这样实现的:
public class items
{
public enum types {
FOO(0),
BAR(1),
BAZ(2);
private final int _value;
types(int value){_value = value;}
public int value(){return _value;}
}
public static types Get(int i)
{
switch(i)
{
case 0:
return types.FOO;
case 1:
return types.BAR;
case 2:
return types.BAZ;
default:
return null;
}
}
public static String Get(Typer type)
{
switch(type)
{
case FOO:
return "This is foo!";
case BAR:
return "This is bar!";
case BAZ:
return "This is baz!";
default:
return "No good!";
}
}
}
有没有更好的方法来处理这个问题?
【问题讨论】: