当前位置:   article > 正文

switch case语句case后的枚举常量不带枚举类型_switch的case后面为什么不能带枚举

switch的case后面为什么不能带枚举

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

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

定义了一个枚举类型

public enum Day
{
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,THURSDAY, FRIDAY, SATURDAY
}
  • 1
  • 2
  • 3
  • 4

在进行switch case分支时

switch (day)
{
    case Day.MONDAY:
        .....
        break;
  • 1
  • 2
  • 3
  • 4
  • 5

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

switch (day)
{
    case MONDAY:
        ......
        break;
  • 1
  • 2
  • 3
  • 4
  • 5

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/729151
推荐阅读
相关标签
  

闽ICP备14008679号