且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我可以在Java中设置枚举起始值吗?

更新时间:2023-02-07 13:36:20

Java枚举不像C或C ++枚举,它们实际上只是整数的标签。

Java enums are not like C or C++ enums, which are really just labels for integers.

Java枚举的实现更像是类 - 它们甚至可以有多个属性。

Java enums are implemented more like classes - and they can even have multiple attributes.

public enum Ids {
    OPEN(100), CLOSE(200);

    private final int id;
    Ids(int id) { this.id = id; }
    public int getValue() { return id; }
}

最大的区别是它们类型安全这意味着您不必担心将COLOR枚举分配给SIZE变量。

The big difference is that they are type-safe which means you don't have to worry about assigning a COLOR enum to a SIZE variable.

请参阅 http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html 了解更多信息。