打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C switch 语句
【面试题】Java单例设计模式-饿汉式枚举(enum)单例
android switch语句case expressions must be constant expressions
枚举类型enum用法
如何使用enum这个数据类型?
编程入门网-VB编程的七个优良习惯
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服