使用枚举值作为字符串文字
使用枚举中存储的值作为字符串文字的最佳方法是什么? 例如:
public enum Modes {
some-really-long-string,
mode1,
mode2,
mode3
}
然后我可以使用 Mode.mode1
将其字符串表示形式返回为 mode1
。无需不断调用 Mode.mode1.toString()
。
What is the best way to use the values stored in an Enum as String literals?
For example:
public enum Modes {
some-really-long-string,
mode1,
mode2,
mode3
}
Then later I could use Mode.mode1
to return its string representation as mode1
. Without having to keep calling Mode.mode1.toString()
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
此方法应该适用于任何
enum
:This method should work with any
enum
:我发现这个更容易防止类型错误:
但是 - 当您需要在 log/println 上使用 String 或每当 java 自动编译 toString() 方法时,这可能会起作用,但在像这样的代码行上 ->
相反,如上所述,在这种情况下,您仍然需要扩展枚举并使用
.name()
:i found this one is more easy for preventing type error:
however - this may work when you need to use a String on a log/println or whenever java compiles the toString() method automatically, but on a code line like this ->
instead as mentioned above you will still have to extend the enum and use
.name()
in those cases like this:使用
mode1.name()
或String.valueOf(Modes.mode1)
use
mode1.name()
orString.valueOf(Modes.mode1)
经过多次尝试我得到了这个解决方案
after many tries I have come with this solution
你可以试试这个:
You can try this:
你不能。我想你这里有四个选择。所有四个都提供了一个解决方案,但方法略有不同...
选项一:在枚举上使用内置的
name()
。如果您不需要任何特殊的命名格式,这完全没问题。选项二: 如果您想要更多控制,请向枚举添加覆盖属性
选项三: 使用静态final代替枚举:
选项四: 接口的每个字段都是公共的、静态的和final的:
You can't. I think you have FOUR options here. All four offer a solution but with a slightly different approach...
Option One: use the built-in
name()
on an enum. This is perfectly fine if you don't need any special naming format.Option Two: add overriding properties to your enums if you want more control
Option Three: use static finals instead of enums:
Option Four: interfaces have every field public, static and final:
每个枚举都有一个
name()
和一个valueOf(String)
方法。前者返回枚举的字符串名称,后者给出名称为字符串的枚举值。这就是您要找的吗?Enum
本身还有一个静态valueOf(Class, String)
,因此您也可以使用:Every enum has both a
name()
and avalueOf(String)
method. The former returns the string name of the enum, and the latter gives the enum value whose name is the string. Is this like what you're looking for?There's also a static
valueOf(Class, String)
onEnum
itself, so you could also use:您可以为每个枚举值重写
toString()
方法。示例:
用法:
You could override the
toString()
method for each enum value.Example:
Usage:
正如 Benny Neugebauer 提到的,您可以覆盖 toString()。然而,我更喜欢这样的内容:
您还可以添加一个静态方法来检索所有字段、打印所有字段等, 而不是覆盖每个枚举字段的 toString。
只需调用 getValue 即可获取与每个 Enum 项关联的字符串
As Benny Neugebauer mentions, you could overwrite the toString(). However instead overwriting the toString for each enum field I like more something like this:
You could also add a static method to retrieve all the fields, to print them all, etc.
Simply call getValue to obtain the string associated to each Enum item
mode1.name()
或String.valueOf(mode1)
。恐怕没有比这更好的了mode1.name()
orString.valueOf(mode1)
. It doesn't get better than that, I'm afraid您可以在任何想要从枚举中获取字符串形式的值的地方进行如下调用。
这将以字符串形式返回“Mode1”。
you can make a call like below wherever you want to get the value as a string from the enum.
This will return "Mode1" as a String.
对于我的枚举,我真的不喜欢为它们每个分配 1 个字符串。这就是我在枚举上实现 toString() 方法的方法。
For my enums I don't really like to think of them being allocated with 1 String each. This is how I implement a toString() method on enums.
据我所知,获得该名称的唯一方法是
如果您确实需要这种方式,但是,您可以这样做:
As far as I know, the only way to get the name would be
If you really need it this way, however, you could do:
您可以使用
Mode.mode1.name()
,但通常不需要这样做。You can use
Mode.mode1.name()
however you often don't need to do this.我的解决方案解决你的问题!
my solution for your problem!
您可以简单地使用:
You can simply use:
它将打印:
This design for 枚举字符串常量适用于大多数情况下。
It will print:
This design for enum string constants works in most of the cases.
枚举只是一个有点特殊的类。枚举可以存储附加字段、实现方法等。例如
现在您可以说:
System.out.println(Modes.mode1.character())
并查看输出:
一个
Enum is just a little bit special class. Enums can store additional fields, implement methods etc. For example
Now you can say:
System.out.println(Modes.mode1.character())
and see output:
a