创建字符串枚举的最佳方法? [英] Best way to create enum of strings?

查看:26
本文介绍了创建字符串枚举的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

enum 类型表示一组字符串的最佳方法是什么?

What is the best way to have a enum type represent a set of strings?

我试过了:

enum Strings{
   STRING_ONE("ONE"), STRING_TWO("TWO")
}

然后我如何将它们用作 Strings ?

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
     */
    Strings(final String text) {
        this.text = text;
    }

    /* (non-Javadoc)
     * @see java.lang.Enum#toString()
     */
    @Override
    public String toString() {
        return text;
    }
}

或者,您可以为 text 创建一个 getter 方法.

Alternatively, you can create a getter method for text.

您现在可以执行 Strings.STRING_ONE.toString();

这篇关于创建字符串枚举的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆