创建字符串枚举的最佳方法是什么?

本文翻译自:Best way to create enum of strings?

What is the best way to have a enum type represent a set of strings? enum类型表示一组字符串的最佳方法是什么?

I tried this: 我试过这个:

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

How can I then use them as Strings ? 我怎样才能将它们用作Strings


#1楼

参考:https://stackoom.com/question/Gh1q/创建字符串枚举的最佳方法是什么


#2楼

Custom String Values for Enum 枚举的自定义字符串值

from http://javahowto.blogspot.com/2006/10/custom-string-values-for-enum.html 来自http://javahowto.blogspot.com/2006/10/custom-string-values-for-enum.html

The default string value for java enum is its face value, or the element name. java enum的默认字符串值是其面值或元素名称。 However, you can customize the string value by overriding toString() method. 但是,您可以通过重写toString()方法来自定义字符串值。 For example, 例如,

public enum MyType {
  ONE {
      public String toString() {
          return "this is one";
      }
  },

  TWO {
      public String toString() {
          return "this is two";
      }
  }
}

Running the following test code will produce this: 运行以下测试代码将产生以下结果:

public class EnumTest {
  public static void main(String[] args) {
      System.out.println(MyType.ONE);
      System.out.println(MyType.TWO);
  }
}


this is one
this is two

#3楼

If you do not want to use constructors , and you want to have a special name for the method, try it this: 如果你希望使用构造器 ,你想拥有的方法特别的名字 ,试试这个:

public enum MyType {
  ONE {
      public String getDescription() {
          return "this is one";
      }
  },    
  TWO {
      public String getDescription() {
          return "this is two";
      }
  };

  public abstract String getDescription();
}

I suspect that this is the quickest solution. 我怀疑这是最快的解决方案。 There is no need to use variables final . 没有必要使用变量final


#4楼

Use its name() method: 使用其name()方法:

public class Main {
    public static void main(String[] args) throws Exception {
        System.out.println(Strings.ONE.name());
    }
}

enum Strings {
    ONE, TWO, THREE
}

yields ONE . 产生ONE


#5楼

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;
    }
}

Alternatively, you can create a getter method for text . 或者,您可以为text创建getter方法。

You can now do Strings.STRING_ONE.toString(); 你现在可以做Strings.STRING_ONE.toString();


#6楼

Either set the enum name to be the same as the string you want or, more generally,you can associate arbitrary attributes with your enum values: 将枚举名称设置为与所需的字符串相同,或者更一般地,您可以将任意属性与枚举值相关联:

enum Strings {
   STRING_ONE("ONE"), STRING_TWO("TWO");
   private final String stringValue;
   Strings(final String s) { stringValue = s; }
   public String toString() { return stringValue; }
   // further methods, attributes, etc.
}

It's important to have the constants at the top, and the methods/attributes at the bottom. 将常量放在顶部,将方法/属性放在底部是很重要的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值