您好,我在数据库中有一个 STATUS 字段作为枚举类型。
但是我如何为此创建设置器?作为字符串、INT 或“Java 枚举”
像这样的东西
public enum getStatus() {
return Status;
}
public void setStatus(enum Status) {
this.Status = Status;
}
您需要使用枚举的名称作为status
. 有些像这样
enum MyEnum { // Assuming MyEnum is the name of the enum
// enum values sample below
ACTIVE, INACTIVE, PENDING
}
编辑:经过我们的讨论,这可能更适合您。
private MyEnum status;
public String getStatus() {
return this.status.name();
}
public void setStatus(String status) {
this.status = MyEnum.valueOf(status);
}
我会看看这是否适合你:
public enum MyStatus { ACTIVE, INACTIVE, PENDING }
public class OtherClass
{
private MyStatus status = null;
/* other code */
/* when it is time to get a value of status to store in the database: */
String statusString = status.name();
/* when it is time to take a string from the database
* and translate to the variable */
status = MyStatus.value(stringFromDatabase);
}
您必须记住,您不能在程序的更高版本中从枚举中删除值,或者您必须记住从数据库中删除所有这些值,或者您必须从 valueOf() 捕获生成的异常并做一些事情智能与它。