将斜杠添加到Java中的枚举 [英] Add slash to an enumeration in Java
问题描述
我需要创建一个枚举
:
public enum STYLE {
ELEMENT1(0), A/R (2)
//Staff
};
但是 Java
不允许这样做。有什么解决方案吗?
感谢
But Java
does not permit this. Is there any solution?
Thanks
推荐答案
java中的枚举只是一个编译时语法糖。实际上枚举
只是一个类,枚举
成员就像 public final static
这个类的字段。每个字段的名称必须遵循某些规则。例如,名称不能包含特殊字符,这意味着该语言的特殊功能。例如 /
不能用作名称的一部分。
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.
您可以将其称为如果你愿意,A_R
如果你还想从枚举成员 A / R
> A_R 你可以定义返回它的方法,例如
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();
}
}
这篇关于将斜杠添加到Java中的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!