编译错误 - 使用枚举切换 [英] Compilation error - switch with enum
本文介绍了编译错误 - 使用枚举切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
有人知道为什么当我切换一个枚举,情况应该是在不合格枚举值?
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是一个特定类型(枚举),但为什么编译器关心如果我使用枚举值的完全限定名称?
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屋!
查看全文