是否可以扩展类并重写封闭的枚举类型?
如果我有一个包含类型的类,是否可以扩展该类并重写枚举类型或向此枚举添加更多常量?目的是用户将能够调用方法,而不知道动态对象是来自基类还是派生类。例如:enum
getColor()
public class Car {
private String name;
private CarColors color;
public enum CarColors {
BLUE,
GREEN
}
public Car(CarColors c) {
color=c;
}
public CarColors getColor() {
return color;
}
}
public class RacingCar extends Car {
private CarColors color;
public RacingCar(CarColors c) {
color=c;
}
public enum CarColors {
BLUE,
GREEN,
RED
}
@Override //?????
public CarColors getColor() {
return color;
}
}
和主要:
Car racingCar = new RacingCar();
CarColors color = racingCar.getColor();