枚举 Java 的最佳方法
我想知道在 Java 中枚举的好方法。
我想知道这个问题很长时间了,通常我会想出一些包含几个独立的枚举和函数以及一些定义等的东西。
但基本上,我想为各种类型的枚举和子枚举定义唯一的数字键。
例如,我正在尝试实现一种具有各种关键字和符号的语言,括号中带有与该元素相对应的数字。就像元素(id)一样。
关键字:program(1)、call(2)、if(3)、else(4)、elsif(5)、... 符号 ';'(6)、','(7)、'='(8)、'+'(9)、... 操作: and(10), or(11), ...
完成此操作的最佳方法是什么?
我希望我的帖子是清楚的。但基本上我正在尝试创建元素类型的类别,然后创建其中具有关联数值的元素定义。
这样做的目的是,如果输入字符串将输入中的子字符串识别为上面“字典”中的元素,则我可以检查输入字符串以返回整数值。
提前致谢!
I'm wondering about a good method to enumerate in Java.
I've wondered this for a long time, and typically I come up with something that includes several independent enums and functions, and some defines and such.
But basically, I want to define unique number keys for various types of enumerations, with sub enumerations.
For example, I'm trying to implement a language with various keywords and symbols, with a number corresponding to that element, in the parenthesis. Like element(id).
Keywords: program(1), call(2), if(3), else(4), elsif(5), ...
Symbols ';'(6), ','(7), '='(8), '+'(9), ...
Operations: and(10), or(11), ...
What would be the best way to accomplish this?
I hope my posting was clear. But basically I'm trying to create categories of element types, and then the element definitions within them, that have an associated numeric value.
The purpose of this would be so that I could, check an input string to return the integer value if it identified substrings in the input as elements in the "dictionary" above.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不应该关心枚举本身的数值,也不应该依赖它们。原因是,如果将来您或其他人决定将新的枚举添加到枚举列表的中间,那么您的所有数值现在都无效,您必须仔细检查代码并更改它们。因此,最好依赖于实际的枚举而不是枚举的数值。
如果您尝试将枚举“分组”在一起(如果我正确理解您的问题),您可以使用类似标记界面的东西。例如,您可以:
然后,您可以使用一个方法,而不是使用两种方法,一种接受 BooleanOperators 类型的枚举,另一种接受 ArithmeticOperators 类型接受类型
Operators
的方法:如果你想将显式值绑定到你的枚举,有一个安全的方法来做到这一点:
那么你可以这样做
ArithmeticOperators.ADD.getCode()
,它将返回与该枚举关联的代码。此方法更安全,因为您可以看到与枚举显式关联的值,而不是根据其定义顺序隐式关联的值。因此,当您或其他人添加新枚举时,他们应该将一个(希望是唯一的)整数值与新枚举关联起来。注意: 在这种情况下,标记接口并不是绝对必要的;它只是强制要求所有实现该接口的枚举都应该具有 getCode() 方法。以下内容同样有效:
我认为这就是您想要的。如果没有,请告诉我!
编辑
根据您的评论,我将使用应用于所有枚举的标记接口(我们称之为
Token
),然后使用类型的映射映射<字符串,令牌>
。您将使用您的令牌和相应的枚举来初始化此映射。当解析器返回标记时,您可以使用映射查找枚举。You shouldn't care about the numeric values of the enums themselves and you shouldn't depend on them. The reason being that, if in the future you or someone else decides to add a new enum to the middle of your list of enums, all your numeric values are now invalid and you'll have to go through the code and change them. Hence it's better to depend on the actual enum rather than the enum's numeric value.
If you're trying to "group" enums together (if I understand your question correctly), you could use something like a marker interface. So for example you could have:
Then instead of having two methods, one that would accept enums of type
BooleanOperators
and another that would accept types ofArithmeticOperators
, you can have a single method that accepts typeOperators
:If you want to tie explicit values to your enums, there is a safe way to do it:
Then you can do
ArithmeticOperators.ADD.getCode()
, which will return the code associated with that enum. This method is safer because you can see the value being explicitly associated with the enum versus being implicitly associated based on their order of definition. So when you, or someone else adds a new enum, they are expected to associate a (hopefully unique) integer value with the new enum.NOTE: The marker interface is not strictly necessary in this case; it just enforces the requirement that all enums that implement the interface should have the
getCode()
method. The following is just as valid:I assume this is what you want. If not, please let me know!
EDIT
Based on your comment, I would use a marker interface (let's call it
Token
) that is applied to all your enums, and then use a map of typeMap<String, Token>
. You will initialize this map with your tokens and the corresponding enum. When your parser returns tokens, you can look up the enum using your map.看一下枚举:
http://download.oracle.com/javase /tutorial/java/javaOO/enum.html
Take a look at enum:
http://download.oracle.com/javase/tutorial/java/javaOO/enum.html
使用
EnumSet
创建枚举
的子集;可以在此处找到示例。Use
EnumSet
to create subsets of yourenum
; an example may be found here.很好的解决方案,但是当您实现运算符时关心它们的优先级,这也可以是枚举参数。我的方法是为每个优先级使用单独的组,当我将它们添加到解析器时,我按优先级顺序添加它们。
Good solution, but when you implement operators care about their priority, which also can be enum parameter. My approach was using separate group for each level of priority, and when i add them to the parser, i add them in order of priority.