枚举 Java 的最佳方法

发布于 2024-12-12 03:43:29 字数 457 浏览 0 评论 0原文

我想知道在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

春花秋月 2024-12-19 03:43:29

您不应该关心枚举本身的数值,也不应该依赖它们。原因是,如果将来您或其他人决定将新的枚举添加到枚举列表的中间,那么您的所有数值现在都无效,您必须仔细检查代码并更改它们。因此,最好依赖于实际的枚举而不是枚举的数值。

如果您尝试将枚举“分组”在一起(如果我正确理解您的问题),您可以使用类似标记界面的东西。例如,您可以:

public interface Operators {
}

public enum BooleanOperators implements Operators {    
   AND, OR, NOT, XOR
}

public enum ArithmeticOperators implements Operators {
   ADD, SUBTRACT, MULTIPLY, DIVIDE
}

然后,您可以使用一个方法,而不是使用两种方法,一种接受 BooleanOperators 类型的枚举,另一种接受 ArithmeticOperators 类型接受类型Operators的方法:

public void doSomethingWithOperators(Operators operators) {
    ....
}

如果你想将显式值绑定到你的枚举,有一个安全的方法来做到这一点:

public interface Operators {
    int getCode();
}

public enum BooleanOperators implements Operators {

   private int code;

   AND(1), OR(2), NOT(3), XOR(4)

   private BooleanOperators(int code) {
       this.code = code;
   }

   public int getCode() {
       return this.code;
   }
}

那么你可以这样做ArithmeticOperators.ADD.getCode(),它将返回与该枚举关联的代码。此方法更安全,因为您可以看到与枚举显式关联的值,而不是根据其定义顺序隐式关联的值。因此,当您或其他人添加新枚举时,他们应该将一个(希望是唯一的)整数值与新枚举关联起来。

注意: 在这种情况下,标记接口并不是绝对必要的;它只是强制要求所有实现该接口的枚举都应该具有 getCode() 方法。以下内容同样有效:

public enum BooleanOperators {

   private int code;

   AND(1), OR(2), NOT(3), XOR(4)

   private BooleanOperators(int code) {
       this.code = code;
   }

   public int getCode() {
       return this.code;
   }
}

我认为这就是您想要的。如果没有,请告诉我!

编辑

根据您的评论,我将使用应用于所有枚举的标记接口(我们称之为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:

public interface Operators {
}

public enum BooleanOperators implements Operators {    
   AND, OR, NOT, XOR
}

public enum ArithmeticOperators implements Operators {
   ADD, SUBTRACT, MULTIPLY, DIVIDE
}

Then instead of having two methods, one that would accept enums of type BooleanOperators and another that would accept types of ArithmeticOperators, you can have a single method that accepts type Operators:

public void doSomethingWithOperators(Operators operators) {
    ....
}

If you want to tie explicit values to your enums, there is a safe way to do it:

public interface Operators {
    int getCode();
}

public enum BooleanOperators implements Operators {

   private int code;

   AND(1), OR(2), NOT(3), XOR(4)

   private BooleanOperators(int code) {
       this.code = code;
   }

   public int getCode() {
       return this.code;
   }
}

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:

public enum BooleanOperators {

   private int code;

   AND(1), OR(2), NOT(3), XOR(4)

   private BooleanOperators(int code) {
       this.code = code;
   }

   public int getCode() {
       return this.code;
   }
}

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 type Map<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.

染火枫林 2024-12-19 03:43:29

使用 EnumSet 创建枚举的子集;可以在此处找到示例。

Use EnumSet to create subsets of your enum; an example may be found here.

白首有我共你 2024-12-19 03:43:29

很好的解决方案,但是当您实现运算符时关心它们的优先级,这也可以是枚举参数。我的方法是为每个优先级使用单独的组,当我将它们添加到解析器时,我按优先级顺序添加它们。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文