我可以在 Java 中设置枚举起始值吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1067352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I set enum start value in Java?
提问by qrtt1
I use the enum to make a few constants:
我使用枚举来创建一些常量:
enum ids {OPEN, CLOSE};
the OPEN value is zero, but I want it as 100. Is it possible?
OPEN 值为零,但我希望它为 100。这可能吗?
采纳答案by lavinio
Java enums are not like C or C++ enums, which are really just labels for integers.
Java 枚举不像 C 或 C++ 枚举,它们实际上只是整数的标签。
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-safewhich means you don't have to worry about assigning a COLOR enum to a SIZE variable.
最大的区别在于它们是类型安全的,这意味着您不必担心将 COLOR 枚举分配给 SIZE 变量。
See http://docs.oracle.com/javase/tutorial/java/javaOO/enum.htmlfor more.
有关更多信息,请参阅http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html。
回答by Paul Morie
Yes. You can pass the numerical values to the constructor for the enum, like so:
是的。您可以将数值传递给枚举的构造函数,如下所示:
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 Guidefor more information.
有关详细信息,请参阅Sun Java 语言指南。
回答by serdar
If you use very big enum types then, following can be useful;
如果您使用非常大的枚举类型,那么以下可能很有用;
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;
}
}
回答by Maher Abuthraa
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:
您可以使用静态方法并将枚举作为参数传递,例如:
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.
请注意,这两种方式使用更少的内存和更多的处理单元。我不是说这是最好的方式,而只是另一种方式。
回答by ggrandes
If you want emulate enum of C/C++ (base num and nexts incrementals):
如果您想模拟 C/C++ 的枚举(基本 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
OPEN 0 100 CLOSE 1 101
回答by maharvey67
The ordinal() function returns the relative position of the identifier in the enum. You can use this to obtain automatic indexing with an offset, as with a C-style enum.
ordinal() 函数返回标识符在枚举中的相对位置。您可以使用它来获取带有偏移量的自动索引,就像使用 C 样式枚举一样。
Example:
例子:
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);
}
};
Gives the output:
给出输出:
OPEN: 100
CLOSE: 101
OTHER: 102
Edit: just realized this is very similar to ggrandes' answer, but I will leave it here because it is very clean and about as close as you can get to a C style enum.
编辑:刚刚意识到这与ggrandes 的答案非常相似,但我将其留在这里,因为它非常干净,并且与 C 风格的枚举尽可能接近。
回答by Gibbs
public class MyClass {
public static void main(String args[]) {
Ids id1 = Ids.OPEN;
System.out.println(id1.getValue());
}
}
enum Ids {
OPEN(100), CLOSE(200);
private final int id;
Ids(int id) { this.id = id; }
public int getValue() { return id; }
}
@scottf, You probably confused because of the constructor defined in the ENUM.
@scottf,您可能因为 ENUM 中定义的构造函数而感到困惑。
Let me explain that.
让我解释一下。
When class loader
loads enum
class, then enum
constructor also called. On what!! Yes, It's called on OPEN
and close
. With what values 100
for OPEN
and 200
for close
当class loader
加载enum
类时,enum
构造函数也被调用。什么!!是的,它被称为OPEN
和close
。有什么价值100
为OPEN
和200
为close
Can I have different value?
我可以有不同的价值吗?
Yes,
是的,
public class MyClass {
public static void main(String args[]) {
Ids id1 = Ids.OPEN;
id1.setValue(2);
System.out.println(id1.getValue());
}
}
enum Ids {
OPEN(100), CLOSE(200);
private int id;
Ids(int id) { this.id = id; }
public int getValue() { return id; }
public void setValue(int value) { id = value; }
}
But, It's bad practice. enum
is used for representing constants
like days of week
, colors in rainbow
i.e such small group of predefined constants.
但是,这是不好的做法。enum
用于表示constants
like days of week
,colors in rainbow
即这样一小组预定义的常量。
回答by sperling
@scottf
@scottf
An enum is like a Singleton. The JVM creates the instance.
枚举就像单例。JVM 创建实例。
If you would create it by yourself with classes it could be look like that
如果你自己用类创建它,它可能看起来像这样
public static class MyEnum {
final public static MyEnum ONE;
final public static MyEnum TWO;
static {
ONE = new MyEnum("1");
TWO = new MyEnum("2");
}
final String enumValue;
private MyEnum(String value){
enumValue = value;
}
@Override
public String toString(){
return enumValue;
}
}
And could be used like that:
并且可以这样使用:
public class HelloWorld{
public static class MyEnum {
final public static MyEnum ONE;
final public static MyEnum TWO;
static {
ONE = new MyEnum("1");
TWO = new MyEnum("2");
}
final String enumValue;
private MyEnum(String value){
enumValue = value;
}
@Override
public String toString(){
return enumValue;
}
}
public static void main(String []args){
System.out.println(MyEnum.ONE);
System.out.println(MyEnum.TWO);
System.out.println(MyEnum.ONE == MyEnum.ONE);
System.out.println("Hello World");
}
}
回答by Coristat
I think you're confused from looking at C++ enumerators. Java enumerators are different.
我认为您对 C++ 枚举器感到困惑。Java 枚举器是不同的。
This would be the code if you are used to C/C++ enums:
如果您习惯于 C/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);
}
};