Java中的isEnum_在Java中开启Enum

实际上,您可以switch使用enums,但是直到Java 7才可以switch使用Strings。您可以考虑将Java enum的多态方法分派使用,而不是显式switch。请注意,enums是Java中的对象,而不仅仅是ints的符号,就像在C / C ++中一样。您可以在enum类型上有一个方法,然后不用编写a switch,只需调用该方法-一行代码即可:完成!

enum MyEnum {

SOME_ENUM_CONSTANT {

@Override

public void method() {

System.out.println("first enum constant behavior!");

}

},

ANOTHER_ENUM_CONSTANT {

@Override

public void method() {

System.out.println("second enum constant behavior!");

}

}; // note the semi-colon after the final constant, not just a comma!

public abstract void method(); // could also be in an interface that MyEnum implements

}

void aMethodSomewhere(final MyEnum e) {

doSomeStuff();

e.method(); // here is where the switch would be, now it's one line of code!

doSomeOtherStuff();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值