为什么不能让枚举在
Java中扩展一些类?
以下示例显示:
>我们可以有正常的枚举E1.
>我们可以拥有“复杂”枚举E2.
>但我们无法通过组合某个类并将其扩展为枚举来获得相同的复杂枚举.
例如:
public class Try_ExtendEnum {
public static enum E1 {
North,
East,
South,
West
}
public static enum E2 {
North(0),
Eash(90),
South(180),
West(270);
private double degrees;
E2(double degrees) {
this.degrees = degrees;
}
public double getDegrees() {
return degrees;
}
}
public static class DegreesMeasure {
private double degrees;
public DegreesMeasure(double degrees) {
this.degrees = degrees;
}
public double getDegrees() {
return degrees;
}
}
public static enum E3 extends DegreesMeasure {
North(0),
Eash(90),
South(180),
West(270);
E3(double degrees) {
super(degrees);
}
}
public static void main(String[] args) {
DegreesMeasure dm = new DegreesMeasure(123);
E3 e = dm; // should not be possible, since E3 is a subclass and not obliged to hold superclass
DegreesMeasure dm2 = E3.Nort; // just holding "tagged" version of instance; see no any problem
}
}