Java中不同枚举类的联合的switch语句
我有两个enum类,比如Enum1和Enum2:Java中不同枚举类的联合的switch语句,java,enums,switch-statement,Java,Enums,Switch Statement,我有两个enum类,比如Enum1和Enum2: enum Enum1 {ONE, TWO, THREE} enum Enum2 {FOUR, FIVE} public <E extends Enum<E>> method (E arg) { switch (arg) { // Here is the compile error -- Cannot switch // on a value of type E.
enum Enum1 {ONE, TWO, THREE}
enum Enum2 {FOUR, FIVE}
public <E extends Enum<E>> method (E arg) {
switch (arg) { // Here is the compile error -- Cannot switch
// on a value of type E. Only convertible int
// values, strings or enum variables are permitted
// (And of course, all the cases are incorrect
// because the enum set is unknown)
case ONE:
// do something
case TWO:
// do something
case THREE:
// do something
case FOUR:
// do something
case FIVE:
// do something
default:
// do something
}
}
我有这样一种方法:
enum Enum1 {ONE, TWO, THREE}
enum Enum2 {FOUR, FIVE}
public <E extends Enum<E>> method (E arg) {
switch (arg) { // Here is the compile error -- Cannot switch
// on a value of type E. Only convertible int
// values, strings or enum variables are permitted
// (And of course, all the cases are incorrect
// because the enum set is unknown)
case ONE:
// do something
case TWO:
// do something
case THREE:
// do something
case FOUR:
// do something
case FIVE:
// do something
default:
// do something
}
}
public方法(E arg){
开关(arg){//这是编译错误--无法切换
//在类型为E的值上。仅可转换int
//允许使用值、字符串或枚举变量
//(当然,所有的案例都是错误的
//因为枚举集未知)
案例一:
//做点什么
案例二:
//做点什么
案例三:
//做点什么
案例四:
//做点什么
案例五:
//做点什么
违约:
//做点什么
}
}
那么,是否可以切换泛型枚举类型的值?
有一种方法可以将其更改为字符串(仅适用于JDK7):
public方法(E arg){
开关(arg.name()){
案例“一”:
//做点什么
案例“二”:
//做点什么
案例“三”:
//做点什么
案例“四”:
//做点什么
案例“五”:
//做点什么
违约:
//做点什么
}
}
您需要在switch语句之前将枚举强制转换为特定的类型(因此每个类型都有一个单独的switch语句)。也就是说,这不是最好的主意。通过重构代码,您可能会找到一个更好的解决方案,这样您就不需要这样做。您不能做您正在尝试的事情。首先,enum
开关实际上是enum的ordinal()
上开关的简写。因此,即使您可以让开关识别您的“联合枚举”类型,该语句也有重复的case
分支。(例如,ONE
和FOUR
都有序号0。)
一种方法可能是将操作移动到枚举本身中。然后,您可以让每个enum
类型实现一个公共接口:
interface Actor {
void doSomething();
}
enum Enum1 implements Actor {
ONE {
public void doSomething() { . . . }
},
TWO {
public void doSomething() { . . . }
},
THREE {
public void doSomething() { . . . }
}
}
enum Enum2 implements Actor {
FOUR {
public void doSomething() { . . . }
},
FIVE {
public void doSomething() { . . . }
}
}
然后,您可以实现您的方法,只需将处理委托给参与者:
public void method(Actor actor) {
if (actor == null) {
// default action
} else {
actor.doSomething();
}
}
你提到的开关显然不起作用
我有一个(非常奇怪的)替换:创建一个“helper enum”,其中包含列出的所有值,并有一个
映射。您看过任何enum文档吗?即使有可能,这两个enum也互不相关。不能针对案例4或案例5值测试Enum1。所以你的代码用这种方式写是没有意义的。@Heisenbug,我知道。我不会在真正的项目中这样做,我只是在尝试这种语言。因为所有的枚举类型都隐式地派生自java.lang.enum,所以我认为不同的枚举类型之间应该有一些通用关系,我试过了。因此,在JDK7中,有一种方法可以做到这一点——打开arg.name()并引用case语句中的所有内容(name()得到一个字符串,该字符串正好表示它在枚举声明中声明的内容)——当然这是一些微不足道的发现,但只是为了好玩@抱歉,我的评论不是要批评你。我只是指出了这一点。总是很高兴看到人们尝试使用这种语言;)是的,我知道。我不会在真正的项目中这样做,我只是在尝试这种语言。因为所有的枚举类型都隐式地派生自java.lang.enum,所以我认为不同的枚举类型之间应该有一些通用关系,我试过了。因此,在JDK7中,有一种方法可以做到这一点——打开arg.name()并引用case语句中的所有内容(name()得到一个字符串,该字符串正好表示它在枚举声明中声明的内容)——当然这是一些微不足道的发现,但只是为了好玩!
switch(HelperEnum.lookUp(a_request)){
case ONE: ....
case TWO: ....
case THREE: ....
case FOUR: ....
case FIVE: ....
}