Java在控制台输入斜杠加数字,将斜杠添加到Java中的枚举

I need to create an enumeration like this:

public enum STYLE {

ELEMENT1(0), A/R (2)

//Staff

};

But Java does not permit this. Is there any solution?

Thanks

解决方案

Enumeration in java is just a compile time syntactic sugar. Actually enum is just a class and enum member is just as public final static field of this class. Each field has name that must follow certain rules. For example name cannot contain special characters that mean something special in the language. For example / cannot be used as a part of name.

You can call it A_R if you want.

If however you want additionally to be able to extract string A/R from enum member A_R you can define method that returns it, e.g.

public enum STYLE {

ELEMENT1(0),

A_R (2) {

@Override

public String printableName() {

return "A_R";

}

};

public String printableName() {

return name();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值