Java:使用整数常量声明枚举时遇到问题
呃,我对 Java 中枚举的工作原理有点困惑。在 C# 和 C++(我通常使用的)中,这似乎没问题,但 Java 想要生我的气>。>
enum Direction
{
NORTH_WEST = 0x0C,
NORTH = 0x10,
NORTH_EAST = 0x14,
WEST = 0x18,
NONE = 0x20,
EAST = 0x28,
SOUTH_WEST = 0x24,
SOUTH = 0x30,
SOUTH_EAST = 0x3C
}
有人可以告诉我我做错了什么吗? 谢谢,
错误如下:
----jGRASP exec: javac -g Test.java
Test.java:79: ',', '}', or ';' expected
NORTH_WEST = 0x0C,
^
Test.java:79: '}' expected
NORTH_WEST = 0x0C,
^
Test.java:80: <identifier> expected
NORTH = 0x10,
^
Test.java:87: ';' expected
SOUTH_EAST = 0x3C
^
Urgh, I'm kind of confused on how enums work in Java. In C# and C++ (what I use normally), this seems okay, but Java wants to get mad at me >.>
enum Direction
{
NORTH_WEST = 0x0C,
NORTH = 0x10,
NORTH_EAST = 0x14,
WEST = 0x18,
NONE = 0x20,
EAST = 0x28,
SOUTH_WEST = 0x24,
SOUTH = 0x30,
SOUTH_EAST = 0x3C
}
Could someone tell me what I'm doing wrong?
Thanks
Here are the errors:
----jGRASP exec: javac -g Test.java
Test.java:79: ',', '}', or ';' expected
NORTH_WEST = 0x0C,
^
Test.java:79: '}' expected
NORTH_WEST = 0x0C,
^
Test.java:80: <identifier> expected
NORTH = 0x10,
^
Test.java:87: ';' expected
SOUTH_EAST = 0x3C
^
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 Java 中,枚举默认不保存任何其他值。您必须创建一个私有字段来存储它。 如有必要,请尝试
添加吸气剂。
In Java enums don't hold any other values by default. You'll have to create a private field to store one. Try something like this
Add getter if necessary.
如果您有java 8并且习惯于拥有自己的工具箱。
然后,您可以添加以下工具:
Java 8 样式界面
从现在开始,您可以这样定义枚举:
然后使用以下方式获取枚举数值:
if you have java 8 and are used to have your own toolbox.
Then, here's kind of tool you can add:
Java 8 style interface
From now on, you can define your enums this way:
Then get the enum numeric values with:
当只需要连续数字而不需要特定值时的快速方法:
用法
Quick way when only consecutive numbers without particular values are needed:
usage
对于这种情况,您似乎可以简单地使用实例字段。
Java
enum
作为对象实现。它们可以有字段和方法。您还可以选择声明一个带有一些参数的构造函数,并在常量声明中为这些参数提供值。您可以使用这些值来初始化任何声明的字段。另请参阅
enum
附录:
EnumSet
和EnumMap
请注意,根据这些值的不同,您可能有比实例字段更好的选择。也就是说,如果您尝试设置位字段的值,则应该仅使用
EnumSet
代替。在 C++ 中,通常会看到两个常量的幂与按位运算 作为集合的紧凑表示。
使用
enum
和EnumSet
,这可能看起来像这样:还有
EnumMap
。这是一个地图
其键是enum
常量。因此,与以前一样,您可能会遇到这样的情况:
现在您可以:
在 Java 中,
enum
是一个非常强大的抽象,它也可以与 Java 集合框架很好地配合使用。另请参阅
enum
代替int
常量EnumSet
而不是位域
EnumMap
而不是顺序索引
相关问题
EnumSet
和EnumMap
用法示例For this scenario, it looks like you can simply use an instance field.
Java
enum
are implemented as objects. They can have fields and methods. You also have the option of declaring a constructor that takes some arguments, and providing values for those arguments in your constant declaration. You can use these values to initialize any declared fields.See also
enum
Appendix:
EnumSet
andEnumMap
Note that depending on what these values are, you may have an even better option than instance fields. That is, if you're trying to set up values for bit fields, you should just use an
EnumSet
instead.It is common to see powers of two constants in, say, C++, to be used in conjunction with bitwise operations as a compact representation of a set.
With
enum
andEnumSet
, this can look something like this:There is also
EnumMap
that you may want to use. It's aMap
whose keys areenum
constants.So, where as before you may have something like this:
Now you can have:
In Java,
enum
is a very powerful abstraction which also works well with the Java Collections Framework.See also
enum
instead ofint
constantsEnumSet
instead of bit fieldsEnumMap
instead of ordinal indexingRelated questions
EnumSet
andEnumMap
usage