100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > (2条消息)switch case语句case后的枚举常量不带枚举类型

(2条消息)switch case语句case后的枚举常量不带枚举类型

时间:2024-07-29 03:16:07

相关推荐

(2条消息)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 :/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 :/javafaq/can-i-use-an-enum-type-in-java-switch-statement/130

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。