我可以在Java中设置枚举起始值吗?
我使用枚举来创建一些常量:
enum ids {OPEN, CLOSE};
OPEN 值为零,但我希望它为 100。这可能吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
Java 枚举与 C 或 C++ 枚举不同,后者实际上只是整数的标签。
Java 枚举的实现更像是类——它们甚至可以有多个属性。
最大的区别是它们是类型安全的,这意味着您不必担心将 COLOR 枚举分配给 SIZE 变量。
请参阅 http://docs.oracle.com/javase/tutorial/java/ javaOO/enum.html 了解更多。
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.
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.
是的。 您可以将数值传递给枚举的构造函数,如下所示:
请参阅 Sun Java 语言指南 了解更多信息。
Yes. You can pass the numerical values to the constructor for the enum, like so:
See the Sun Java Language Guide for more information.
使用这种方式怎么样:
只有一种方法..
您可以使用静态方法并将枚举作为参数传递
像:
请注意,这两种方法使用更少的内存和更多的进程单元..我并不是说这是最好的方法,但它只是另一种方法。
whats about using this way:
there is only one method ..
you can use static method and pass the Enum as parameter
like:
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.
如果您使用非常大的枚举类型,那么以下内容可能很有用;
If you use very big enum types then, following can be useful;
如果你想模拟 C/C++ 的枚举(基数和下一个增量):
If you want emulate enum of C/C++ (base num and nexts incrementals):
ordinal() 函数返回枚举中标识符的相对位置。 您可以使用它来获得带有偏移量的自动索引,就像 C 样式枚举一样。
示例:
给出输出:
编辑:刚刚意识到这与 ggrandes 的答案非常相似,但我会离开放在这里是因为它非常干净并且尽可能接近 C 风格的枚举。
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.
Example:
Gives the output:
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.
@scottf
枚举就像单例。 JVM 创建实例。
如果您自己使用类创建它,它可能看起来像这样
并且可以像这样使用:
@scottf
An enum is like a Singleton. The JVM creates the instance.
If you would create it by yourself with classes it could be look like that
And could be used like that:
@scottf,您可能会因为 ENUM 中定义的构造函数而感到困惑。
让我解释一下。
当
class loader
加载enum
类时,也会调用enum
构造函数。 什么!! 是的,它在OPEN
和close
时调用。OPEN
的值为100
,close
的值为200
我可以有不同的值吗?
是的,
但是,这是不好的做法。
enum
用于表示常量
,例如星期几
、彩虹颜色
,即一小组预定义常量。@scottf, You probably confused because of the constructor defined in the ENUM.
Let me explain that.
When
class loader
loadsenum
class, thenenum
constructor also called. On what!! Yes, It's called onOPEN
andclose
. With what values100
forOPEN
and200
forclose
Can I have different value?
Yes,
But, It's bad practice.
enum
is used for representingconstants
likedays of week
,colors in rainbow
i.e such small group of predefined constants.我认为您对 C++ 枚举器感到困惑。 Java 枚举器是不同的。
如果您习惯使用 C/C++ 枚举,则代码如下:
I think you're confused from looking at C++ enumerators. Java enumerators are different.
This would be the code if you are used to C/C++ enums: