我如何在项目中使用枚举?

时间:2017-06-05 09:14:37

标签: java enums ejb enumeration

我有一个带有实例字段lockAction的类,值1是锁定,3是解锁。我想在我的EJB项目中引入枚举。我怎么做到这一点?

public enum lockUnlock {
    LOCK, //1
    UNLOCK, //3

}

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)

@eldix_当您知道代码中的某些常量数据无法被用户更改时,您可以使用枚举。 例如。 如果你想在屏幕下拉列表中显示一些数据,如下所示

enter image description here

如示例所示,其中下拉值是常量,客户端必须选择。

我们可以在一个地方更改它,在任何地方使用它,而不会更改数据。 enter image description here

相关问题