enum유형이 문자열 집합을 나타내는 가장 좋은 방법은 무엇입니까 ?
나는 이것을 시도했다 :
enum Strings{
   STRING_ONE("ONE"), STRING_TWO("TWO")
}그런 다음 어떻게 그들을 사용할 수 Strings있습니까?
답변
나는 당신이 무엇을하고 싶은지 모르겠지만 이것이 실제로 예제 코드를 번역 한 방법입니다 ….
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;
    }
}또는에 대한 getter 메소드를 작성할 수 있습니다 text.
이제 할 수 있습니다 Strings.STRING_ONE.toString();
답변
열거 형의 사용자 정의 문자열 값
에서 http://javahowto.blogspot.com/2006/10/custom-string-values-for-enum.html
java enum의 기본 문자열 값은 액면 값 또는 요소 이름입니다. 그러나 toString () 메서드를 재정 의하여 문자열 값을 사용자 지정할 수 있습니다. 예를 들어
public enum MyType {
  ONE {
      public String toString() {
          return "this is one";
      }
  },
  TWO {
      public String toString() {
          return "this is two";
      }
  }
}다음 테스트 코드를 실행하면 다음이 생성됩니다.
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답변
그 name()방법을 사용하십시오 :
public class Main {
    public static void main(String[] args) throws Exception {
        System.out.println(Strings.ONE.name());
    }
}
enum Strings {
    ONE, TWO, THREE
}수율 ONE.
답변
열거 형 이름을 원하는 문자열과 동일하게 설정하거나보다 일반적으로 임의의 속성을 열거 형 값과 연결할 수 있습니다.
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.
}상수는 맨 위에 있고 메소드는 속성을 맨 아래에 두는 것이 중요합니다.
답변
“문자열로 사용”의 의미에 따라 여기에서 열거 형을 사용하지 않을 수 있습니다. 대부분의 경우, Elite Gentleman이 제안한 솔루션을 사용하면 in System.out.println(STRING_ONE)또는 to와 같은 toString 메소드를 통해 사용할 수 String s = "Hello "+STRING_TWO있지만 실제로 String (예 :)이 필요한 경우 STRING_ONE.toLowerCase()상수로 정의하는 것이 좋습니다.
public interface Strings{
  public static final String STRING_ONE = "ONE";
  public static final String STRING_TWO = "TWO";
}답변
문자열 Enum에 사용할 수 있습니다
public enum EnumTest {
    NAME_ONE("Name 1"),
    NAME_TWO("Name 2");
    private final String name;
    /**
     * @param name
     */
    private EnumTest(final String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
}그리고 주요 메소드에서 호출
public class Test {
    public static void main (String args[]){
        System.out.println(EnumTest.NAME_ONE.getName());
        System.out.println(EnumTest.NAME_TWO.getName());
    }
}답변
당신이 경우 하지 않습니다 사용할 생성자를 , 그리고 당신이해야 할 특별한 이름을 방법, 그것은이 시도 :
public enum MyType {
  ONE {
      public String getDescription() {
          return "this is one";
      }
  },
  TWO {
      public String getDescription() {
          return "this is two";
      }
  };
  public abstract String getDescription();
}이것이 가장 빠른 해결책 이라고 생각합니다 . final 변수 를 사용할 필요가 없습니다 .
