选择正确的枚举
我有许多枚举,每个枚举都包含要测试的属性名称。我遇到的问题是如何为对象选择相关的枚举。如何仅定义一个在整个代码中使用的枚举变量,该变量可以通过 initalise 方法进行设置。
编辑:
很抱歉回复延迟。我不得不离开办公桌,
这很可能是糟糕的设计。我有一些枚举,如下所示:
public enum AccountGrpEnum {
Account("Account"),
AccountType("AccountType"),
AcctIDSource("AcctIDSource");
private static Set<String> grpNames = new HashSet<String>(3) {{
for(AccountGrpEnum e : AccountGrpEnum.values()) {
add(e.toString());
}
}};
public static boolean contains(String name) {
return grpNames.contains(name);
}
private String name;
private AccountGrpEnum(String name) {
this.name = name;
}
public String toString() {
return this.name;
}
}
另一个 Enum
:
public enum BlockManValEnum {
avgPx("avgPx"),
quantity("quantity"),
securityIDSource("securityIDSource"),
securityID("securityID"),
blockStatus("blockStatus"),
side("side");
private static Set<String> names = new HashSet<String>(9) {{
for(BlockManValEnum e : BlockManValEnum.values()) {
add(e.toString());
}
}};
public static boolean contains(String name) {
return names.contains(name);
}
private String name;
private BlockManValEnum(String name) {
this.name = name;
}
public String toString() {
return this.name;
}
}
在我的代码中,我检查传入对象的字段以查看它们是否包含在枚举中。如下:
if (BlockManValEnum.contains(fields[i].getName()))
但是我希望它符合
if (variableEnum.contains(fields[i].getName()))
Where variableEnum
can be set at runtime的方式。
希望这更清楚伙计们
I have a number of Enums each of which contain the names of attributes to be tested. The problem I have is how to select the relevant enum for the object. How can I define just a Enum variable which use throughout my code which can be set through an initalise method.
EDIT:
Sorry for the delayed reponse. I had to step away from the desk
It very well be bad design. I have a few enums as follows:
public enum AccountGrpEnum {
Account("Account"),
AccountType("AccountType"),
AcctIDSource("AcctIDSource");
private static Set<String> grpNames = new HashSet<String>(3) {{
for(AccountGrpEnum e : AccountGrpEnum.values()) {
add(e.toString());
}
}};
public static boolean contains(String name) {
return grpNames.contains(name);
}
private String name;
private AccountGrpEnum(String name) {
this.name = name;
}
public String toString() {
return this.name;
}
}
Another Enum
:
public enum BlockManValEnum {
avgPx("avgPx"),
quantity("quantity"),
securityIDSource("securityIDSource"),
securityID("securityID"),
blockStatus("blockStatus"),
side("side");
private static Set<String> names = new HashSet<String>(9) {{
for(BlockManValEnum e : BlockManValEnum.values()) {
add(e.toString());
}
}};
public static boolean contains(String name) {
return names.contains(name);
}
private String name;
private BlockManValEnum(String name) {
this.name = name;
}
public String toString() {
return this.name;
}
}
Within my code I am checking the fields of an incoming object to see they are contained within the Enum. As follows:
if (BlockManValEnum.contains(fields[i].getName()))
however I would like it to be along the lines of
if (variableEnum.contains(fields[i].getName()))
Where variableEnum
can be set at runtime.
Hope this is clearer guys
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基于之前的答案。
请记住,简单地说,枚举只是一个类,因此您可以向它们添加任何您想要的方法。这是否是一个好主意取决于具体情况
Building on previous answers.
Remember that simplistically, enums are just a class, so you can add any methods you want to them. Whether or not it's a good idea depends on circumstance
使用
Enum.valueOf
:Use
Enum.valueOf
:由于枚举是类,因此可以实现接口,因此您可以创建一个包含 contains() 方法的接口,然后在枚举上实现该方法,然后使用通用方法它采用实现该接口(并且可以在运行时设置)的特定枚举类型的类标记来进行测试。像这样的东西:
CanBeTestedForContains:
ColorEnum:
SuitEnum:
ContainsSelectorTest:
输出:
Since enums are classes and thus can implement interfaces, you could create an interface which holds your contains() method and then implement that method on your enums, then use a generic method which takes a class token of a specific enum type implementing that interface (and which could be set at runtime) to test. Something like this:
CanBeTestedForContains:
ColorEnum:
SuitEnum:
ContainsSelectorTest:
Output: