java枚举不能0开头_为什么enum不能在Java中扩展类?

为什么不能让枚举在

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

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值