创建字符串枚举的最佳方式? [英] Best way to create enum of strings?
本文介绍了创建字符串枚举的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$ enum
类型代表一组字符串的最佳方法是什么?
What is the best way to have a enum
type represent a set of strings?
我尝试过:
I tried this:
enum Strings{
STRING_ONE("ONE"), STRING_TWO("TWO")
}
然后如何使用它们作为字符串
?
How can I then use them as Strings
?
推荐答案
我不知道你想做什么,但这是我实际翻译了你的示例代码... 。
I don't know what you want to do, but this is how I actually translated your example code....
/**
*
*/
package test;
/**
* @author The Elite Gentleman
*
*/
public enum Strings {
STRING_ONE("ONE"),
STRING_TWO("TWO")
;
private final String text;
/**
* @param text
*/
private Strings(final String text) {
this.text = text;
}
/* (non-Javadoc)
* @see java.lang.Enum#toString()
*/
@Override
public String toString() {
return text;
}
}
或者,您可以为 text
。
现在可以执行 Strings.STRING_ONE.toString();
这篇关于创建字符串枚举的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文