编译错误 - 使用枚举切换 [英] Compilation error - switch with enum

查看:152
本文介绍了编译错误 - 使用枚举切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

为什么我得到一个Enum常量引用不能在case标签中被限定?


有人知道为什么当我切换一个枚举时,这些情况应该是不合格的枚举值?

Hi, Does someone knows why when I switch over an Enum, the cases should be on the unqualified Enum value?

示例:

switch(var) {

case Enum.FIRST:

break;

}

是非法的

但是:

switch(var) {

case FIRST:

break;

}

是合法的。

我知道var是一个特定的类型(枚举),但是为什么编译器关心我是否使用了Enum值的完全限定名称?

I understand that var is of a specific type (Enum) but why the compiler cares if I use the fully qualified name of the Enum value?

推荐答案

因为Java语言规范表明它是这样的。

Because the Java Language Specification states that it is so.

具体来说,对SwitchLabel的定义:

Specifically, the defintition of a SwitchLabel:

SwitchLabel:
        case ConstantExpression :
        case EnumConstantName :
        default :

请参阅 http://java.sun.com/docs/books/jls/third_edition/html/statements.html#258896

这篇关于编译错误 - 使用枚举切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆