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

qrt*_*tt1 177 java enums

我使用枚举来制作一些常量:

enum ids {OPEN, CLOSE};
Run Code Online (Sandbox Code Playgroud)

OPEN值为零,但我希望它为100.是否可能?

lav*_*nio 293

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

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

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

    private final int id;
    Ids(int id) { this.id = id; }
    public int getValue() { return id; }
}
Run Code Online (Sandbox Code Playgroud)

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

有关更多信息,请参见http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html.

  • Java标准的懒惰是不允许为枚举设置值。至少c#允许此AND类型安全。 (2认同)
  • FWIW,C++ 11现在有一个类型安全的"枚举类". (2认同)

Pau*_*rie 88

是.您可以将数值传递给枚举的构造函数,如下所示:

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

  private int value;    

  private Ids(int value) {
    this.value = value;
  }

  public int getValue() {
    return value;
  }
}
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参见" Sun Java语言指南".


Mah*_*raa 14

关于使用这种方式的最新消息:

public enum HL_COLORS{
          YELLOW,
          ORANGE;

          public int getColorValue() {
              switch (this) {
            case YELLOW:
                return 0xffffff00;
            case ORANGE:
                return 0xffffa500;    
            default://YELLOW
                return 0xffffff00;
            }
          }
}
Run Code Online (Sandbox Code Playgroud)

只有一种方法..

您可以使用静态方法并将Enum作为参数传递,如:

public enum HL_COLORS{
          YELLOW,
          ORANGE;

          public static int getColorValue(HL_COLORS hl) {
              switch (hl) {
            case YELLOW:
                return 0xffffff00;
            case ORANGE:
                return 0xffffa500;    
            default://YELLOW
                return 0xffffff00;
            }
          }
Run Code Online (Sandbox Code Playgroud)

请注意,这两种方式使用更少的内存和更多的处理单元.我不是说这是最好的方法,但它只是另一种方法.


小智 10

如果你使用非常大的枚举类型,那么以下内容可能很有用;

public enum deneme {

    UPDATE, UPDATE_FAILED;

    private static Map<Integer, deneme> ss = new TreeMap<Integer,deneme>();
    private static final int START_VALUE = 100;
    private int value;

    static {
        for(int i=0;i<values().length;i++)
        {
            values()[i].value = START_VALUE + i;
            ss.put(values()[i].value, values()[i]);
        }
    }

    public static deneme fromInt(int i) {
        return ss.get(i);
    }

    public int value() {
    return value;
    }
}
Run Code Online (Sandbox Code Playgroud)


ggr*_*des 6

如果要模拟C / C ++的枚举(基数和下一个增量):

enum ids {
    OPEN, CLOSE;
    //
    private static final int BASE_ORDINAL = 100;
    public int getCode() {
        return ordinal() + BASE_ORDINAL;
    }
};

public class TestEnum {
    public static void main (String... args){
        for (ids i : new ids[] { ids.OPEN, ids.CLOSE }) {
            System.out.println(i.toString() + " " + 
                i.ordinal() + " " + 
                i.getCode());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)
OPEN 0 100
CLOSE 1 101
Run Code Online (Sandbox Code Playgroud)


mah*_*y67 5

ordinal() 函数返回枚举中标识符的相对位置。您可以使用它来获得带有偏移量的自动索引,就像 C 样式枚举一样。

例子:

public class TestEnum {
    enum ids {
        OPEN,
        CLOSE,
        OTHER;

        public final int value = 100 + ordinal();
    };

    public static void main(String arg[]) {
        System.out.println("OPEN:  " + ids.OPEN.value);
        System.out.println("CLOSE: " + ids.CLOSE.value);
        System.out.println("OTHER: " + ids.OTHER.value);
    }
};
Run Code Online (Sandbox Code Playgroud)

给出输出:

OPEN:  100
CLOSE: 101
OTHER: 102
Run Code Online (Sandbox Code Playgroud)

编辑:刚刚意识到这与ggrandes 的答案非常相似,但我将其留在这里,因为它非常干净,并且与 C 风格枚举尽可能接近。