可以在java中设置枚举起始值 [英] can set enum start value in java
本文介绍了可以在java中设置枚举起始值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用枚举做一些常量:
枚举ids {OPEN,CLOSE};
OPEN值为零,但我希望它为100.是可能吗?
解决方案
Java枚举不像C或C ++枚举,它们只是整数的标签。
pre pre pre pre pre code $ b OPEN(100),CLOSE(200);
private final int id;
Ids(int id){this.id = id; }
public int getValue(){return id; }
}
最大的区别在于它们是类型安全这意味着你不必担心将COLOR枚举分配给SIZE变量。
请参阅 http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html 了解更多信息。 p>
I use the enum make a few constants:
enum ids {OPEN, CLOSE};
the OPEN value is zero, but I want it as 100. Is it possible?
解决方案
Java enums are not like C or C++ enums, which are really just labels for integers.
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; }
}
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.
See http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html for more.
这篇关于可以在java中设置枚举起始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文