我正在研究 switch 表达式,我想我发现了一个奇怪的行为:
public static boolean isTimeToParty(Day day) {
switch (day) {
case MONDAY -> dog(); //expression allowed
case TUESDAY -> 2 + 2; //error: not a statement
case WEDNESDAY -> true; //error: not a statement
default -> System.out.println("");
}
return false;
}
public static int dog() {
return 2;
}
为什么我可以键入作为dog()
表达式的值,但不允许使用其他类型的表达式?Intellij 提示我"not a statement"
错误。
提前致谢。