使用OpenJDK 14.0.1
public class Example {
private String test(final ExampleEnum ee) {
return switch (ee) {
case Value -> null;
};
}
}
public enum ExampleEnum {
Value;
public enum InnerEnum {
}
}
编译失败,“开关表达式未涵盖所有可能的输入值”。如果我从InnerEnum
中删除ExampleEnum
,则会编译代码。为什么此内部枚举的存在会导致switch表达式失败?有逻辑解释还是编译器错误?
您需要添加默认大小写,如下所示:
public class Example {
private String test(final ExampleEnum ee) {
return switch (ee) {
case Value -> null;
default -> throw new IllegalStateException("Unexpected value");
};
}