有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java:在子类下使用带有enum的switch语句

首先,我要说的是,我对C#中的枚举更为熟悉,而java中的枚举似乎相当混乱

如您所见,在下一个示例中,我尝试使用switch语句@enums,但无论我做什么,我总是会遇到错误

我收到的错误是:

The qualified case label SomeClass.AnotherClass.MyEnum.VALUE_A must be replaced with the unqualified enum constant VALUE_A

问题是我非常理解这个错误,但是我不能只写值,因为枚举位于另一个子类中。有没有办法解决这个问题?为什么它会发生在Java中

//Main Class
public class SomeClass {

    //Sub-Class
    public static class AnotherClass {
        public enum MyEnum {
            VALUE_A, VALUE_B
        }    
        public MyEnum myEnum;
    }

    public void someMethod() { 
        MyEnum enumExample //...

        switch (enumExample) {
            case AnotherClass.MyEnum.VALUE_A: { <-- error on this line
                //..
                break;
            }
        }
    }
}

共 (6) 个答案

  1. # 1 楼答案

    Java自动推断case中元素的类型,因此标签必须是非限定的

    int i;
    switch(i) {
       case 5: // <- integer is expected
    }
    MyEnum e;
    switch (e) {
       case VALUE_A: // <- an element of the enumeration is expected
    }
    
  2. # 2 楼答案

    这就是我使用它的方式。而且它工作得非常好-

    public enum Button {
            REPORT_ISSUES(0),
            CANCEL_ORDER(1),
            RETURN_ORDER(2);
    
            private int value;
    
            Button(int value) {
                this.value = value;
            }
    
            public int getValue() {
                return value;
            }
        }
    

    switch-case如下所示

    @Override
    public void onClick(MyOrderDetailDelgate.Button button, int position) {
        switch (button) {
            case REPORT_ISSUES: {
                break;
            }
            case CANCEL_ORDER: {
                break;
            }
            case RETURN_ORDER: {
                break;
            }
        }
    }
    
  3. # 3 楼答案

    将其更改为:

    switch (enumExample) {
        case VALUE_A: {
            //..
            break;
        }
    }
    

    线索在错误中。您不需要使用枚举类型限定case标签,只需限定其值即可

  4. # 4 楼答案

    Java14开始,可以使用开关表达式

    这个职位

    public enum MyEnum {
        VALUE_A, VALUE_B;
    }
    
    public void someMethod() { 
        MyEnum enumExample //...
    
        switch (enumExample) {
            case VALUE_A -> {
                // logic
            }
            case VALUE_B -> {
                // logic
            }   
        }
    }
    

    开关表达式

    Like all expressions, switch expressions evaluate to a single value and can be used in statements. They may contain "case L ->" labels that eliminate the need for break statements to prevent fall through. You can use a yield statement to specify the value of a switch expression.

    public enum Month {
        JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC;
    }
    

    示例1:返回值

    public static int getNoOfDaysInAMonth(Month month, boolean isLeapYear) {
        return switch(month) {
            case APR, JUN, SEP, NOV -> 30;
            case FEB -> (isLeapYear)? 29: 28;
            case JAN, MAR, MAY, JUL, AUG, OCT, DEC -> 31;
        };
    }
    

    示例2:不返回值

    public static void printNoOfDaysInAMonth(Month month, boolean isLeapYear) {
        switch(month) {
            case APR, JUN, SEP, NOV -> {
                System.out.println("30 days");
            }
            case FEB -> {
                System.out.println(((isLeapYear)? 29: 28) + " days");
            }
            case JAN, MAR, MAY, JUL, AUG, OCT, DEC -> {
                System.out.println("31 days");
            }
        };
    }
    

    参考文献

    Switch Expressions

  5. # 5 楼答案

    错:

    case AnotherClass.MyEnum.VALUE_A
    

    对:

    case VALUE_A:
    
  6. # 6 楼答案

    这应该做到:

    //Main Class
    public class SomeClass {
    
        //Sub-Class
        public static class AnotherClass {
            public enum MyEnum {
                VALUE_A, VALUE_B
            }    
            public MyEnum myEnum;
        }
    
        public void someMethod() { 
            AnotherClass.MyEnum enumExample = AnotherClass.MyEnum.VALUE_A; //...
    
            switch (enumExample) {
                case VALUE_A: { //<-- error on this line
                //..
                break;
                }
            }
        }
    }