An enum switch case label must be the unqualified name of an enumeration constant

Bug && fix

switch case代码块中,我欲对enum类型的对象做case区分。可是报错:
在这里插入图片描述
就是说 case 后面的这个label需要改一下,不能这样写(枚举类型名.枚举值)。
那要怎么写?去掉 枚举类型名. 只写 枚举值 即可。

在这里插入图片描述
即使是你自己定义的枚举类,也是一样的用法。

Java switch 官方文档

首先上官网文档: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
以下会对部分文档内容作翻译和分析整理。

  1. switch代码块支持判断的变量类型都有哪些?
The switch Statement

A switch works with the byte, short, char, and int primitive data types. 
It also works with enumerated types (discussed in Enum Types), the String class, 
and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer (discussed in Numbers and Strings).

基本类型: byte short char int
字符串String 枚举类型 enum
特殊的类型(对4种支持的基本类型做了封装的类):Byte Short Character Integer
也就是说,共有 4+2+4 即10种类型。

  1. String类型是从JDK 1.7开始支持的(switch代码块)。
Using Strings in switch Statements
In Java SE 7 and later, you can use a String object in the switch statement's expression. 

猜你喜欢

转载自blog.csdn.net/wuyujin1997/article/details/130911243