switch case语句case后的枚举常量不带枚举类型

switch case语句case后的枚举常量不带枚举类型

switch case语句case后的枚举常量不带枚举类型

定义了一个枚举类型

public enum Day
{
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,THURSDAY, FRIDAY, SATURDAY
}

在进行switch case分支时

switch (day)
{
    case Day.MONDAY:
        .....
        break;

报错,an enum switch case label must be the unqualified name of an enumeration constant
提示需要把MONDAY前面的枚举类型去掉

switch (day)
{
    case MONDAY:
        ......
        break;

java规定case后面的枚举常量名只能使用unqualified name,switch后已经指定了枚举的类型,case后无需使用全名,而且enum也不存在继承关系

If the type of the switch statement’s Expression is an enum type, then every case constant associated with the switch statement must be an enum constant of that type.

from : http://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.11

the enum value in the case switch label must be an unqualified name. It inherits the context from the type of the object in the switch() clause, and that cases can simply use unqualified names.

from : http://www.xyzws.com/javafaq/can-i-use-an-enum-type-in-java-switch-statement/130

你可能感兴趣的:(Java)