我有一个带有实例字段lockAction的类,值1是锁定,3是解锁。我想在我的EJB项目中引入枚举。我怎么做到这一点?
public enum lockUnlock {
LOCK, //1
UNLOCK, //3
}
答案 0 :(得分:3)
you can use something like this.
public enum lockUnlock {
LOCK(1), UNLOCK(3);
int value;
lockUnlock(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
class Test {
public static void main(String[] args) {
lockUnlock[] b = lockUnlock.values();
for (lockUnlock b1 : b) {
System.out.println(b1 + "........" + b1.getValue());
}
}
}
答案 1 :(得分:2)
您可以像这样为枚举赋值。
public enum LockUnlock {
LOCK(1), UNLOCK(3);
private final int value;
private LockUnlock(int value) {
this.value = value;
}
public int getValue() { return value; }
}
答案 2 :(得分:0)