开发者

How to use enum to define a bunch of string consts

any example of using enum to define a bunch of string const开发者_如何转开发s? The string can contains special charaters like - / etc?


enum MyConstants {
  STR1("some text"),
  STR2("some other text");

  private String value;
  private MyConstants(String str) {
    this.value = str;
  }
  public String getValue() {
    return value;
  }
}

then use it like this:

MyConstants.STR1.getValue();


String [] messages = {"maybe you", "better go with", "an array?"};
System.out.println (messages[1]);

Without further knowledge - why do you like to use enums at all?


I think this page will be helpful: http://javahowto.blogspot.com/2006/10/custom-string-values-for-enum.html

In short:

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜