对于最佳实践,最好使用下面的代码return "red"
来模拟未知值?或者我应该添加一个unknown("red")
选项并执行return Shape.unknown.color;
?关于未知值的枚举是否有标准的 Java 约定?
private enum Shape {
triangle("yellow"),
square("green"),
circle("red");
private final String color;
Shape(String color) {
this.color = color;
}
};
public String getShapeColor() {
if(shape != null) {
return shape.color;
}
return "red";
}