How to match and switch Strings against enums?
This question already has an answer here:
首先, Letter
必须是一个枚举:
enum Letter {
A, B, C
}
Letter letter = Letter.valueOf("A")
// and just switch
switch (letter) {
case A:
case B:
case C:
}
You can do it like this:
String letter = "A";
switch (Letter.valueOf(letter)) {
case A: // No problem!
case B:
case C:
default:
}
Demo on ideone.
首先让该枚举代替类替换为此
Letter obj = Letter.valueOf(letter);
switch (obj) {
case A: //cannot convert from Letter to String
case B: //case expressions must be constant expressions
case C: //case expressions must be constant expressions
default:
链接地址: http://www.djcxy.com/p/38090.html
上一篇: 获取Enum实例
下一篇: 如何匹配并将字符串切换为枚举?