Bug && fix
在switch case
代码块中,我欲对enum
类型的对象做case区分。可是报错:
就是说 case
后面的这个label需要改一下,不能这样写(枚举类型名.枚举值)。
那要怎么写?去掉 枚举类型名. 只写 枚举值 即可。
即使是你自己定义的枚举类,也是一样的用法。
Java switch 官方文档
首先上官网文档: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
以下会对部分文档内容作翻译和分析整理。
- 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种类型。
- 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.