我可以在Java中设置enum start值吗?

时间:2022-11-14 20:48:10

I use the enum to make a few constants:

我使用enum作一些常量:

enum ids {OPEN, CLOSE};

the OPEN value is zero, but I want it as 100. Is it possible?

开放值是0,但我想要它是100。是可能的吗?

5 个解决方案

#1


264  

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

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

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

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

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

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

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.

最大的区别在于它们是类型安全的,这意味着您不必担心为大小变量分配颜色enum。

See http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html for more.

见http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html。

#2


77  

Yes. You can pass the numerical values to the constructor for the enum, like so:

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

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

  private int value;    

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

  public int getValue() {
    return value;
  }
}

See the Sun Java Language Guide for more information.

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

#3


10  

If you use very big enum types then, following can be useful;

如果您使用非常大的enum类型,那么以下内容可能会很有用;

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;
    }
}

#4


10  

whats about using this way:

用这种方式怎么样:

public enum HL_COLORS{
          YELLOW,
          ORANGE;

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

there is only one method ..

只有一种方法。

you can use static method and pass the Enum as parameter like:

您可以使用静态方法,将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;
            }
          }

Note that these two ways use less memory and more process units .. I don't say this is the best way but its just another approach.

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

#5


3  

If you want emulate enum of C/C++ (base num and nexts incrementals):

如果您想要模拟C/ c++的enum(基数num和nexts增量):

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());
        }
    }
}
OPEN 0 100
CLOSE 1 101

#1


264  

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

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

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

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

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

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

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.

最大的区别在于它们是类型安全的,这意味着您不必担心为大小变量分配颜色enum。

See http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html for more.

见http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html。

#2


77  

Yes. You can pass the numerical values to the constructor for the enum, like so:

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

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

  private int value;    

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

  public int getValue() {
    return value;
  }
}

See the Sun Java Language Guide for more information.

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

#3


10  

If you use very big enum types then, following can be useful;

如果您使用非常大的enum类型,那么以下内容可能会很有用;

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;
    }
}

#4


10  

whats about using this way:

用这种方式怎么样:

public enum HL_COLORS{
          YELLOW,
          ORANGE;

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

there is only one method ..

只有一种方法。

you can use static method and pass the Enum as parameter like:

您可以使用静态方法,将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;
            }
          }

Note that these two ways use less memory and more process units .. I don't say this is the best way but its just another approach.

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

#5


3  

If you want emulate enum of C/C++ (base num and nexts incrementals):

如果您想要模拟C/ c++的enum(基数num和nexts增量):

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());
        }
    }
}
OPEN 0 100
CLOSE 1 101