switch
语句或表达式的 switch
语句。
示例:
double getPrice(String fruit) {
// Switch 语句可以替换为增强的 'switch'
switch (fruit) {
case "Apple":
return 1.0;
case "Orange":
return 1.5;
case "Mango":
return 2.0;
default:
throw new IllegalArgumentException();
}
}
在应用快速修复后:
double getPrice(String fruit) {
return switch (fruit) {
case "Apple" -> 1.0;
case "Orange" -> 1.5;
case "Mango" -> 2.0;
default -> throw new IllegalArgumentException();
};
}
该检查仅适用于 14 级或更高的语言级别
2019.1 的新功能