相关疑难解决方法(0)

用于Java中不同枚举类的联合的switch语句

我有两个枚举类,比如Enum1和Enum2:

enum Enum1 {ONE, TWO, THREE}
enum Enum2 {FOUR, FIVE}
Run Code Online (Sandbox Code Playgroud)

我有一个像这样的方法:

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 …
Run Code Online (Sandbox Code Playgroud)

java enums switch-statement

2
推荐指数
1
解决办法
1529
查看次数

标签 统计

enums ×1

java ×1

switch-statement ×1