开发者

How to put {,},(,) in enum

开发者 https://www.devze.com 2023-04-12 04:26 出处:网络
I am trying to group bunch of bracts in java enum. Can some one tell me how to do this? I tried following

I am trying to group bunch of bracts in java enum. Can some one tell me how to do this? I tried following

enum sample{'{','}';}
enum sample{"{"开发者_开发问答,"}";}
enum sample{{,};}

none of them compiles.


Don't forget that enum can have a custom constructor:

public enum Bracket {
  OPEN_BRACKET('{'), CLOSE_BRACKET('}');

  private final char symbol;

  Brackets(char symbol) {
      this.symbol = symbol;
  }

  public char getSymbol() {
      return symbol;
  }    
}


You can also create an enum with custom toString() methods that will return your custom strings:

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

TWO {
    public String toString() {
        return "}";
    }
}
}


enum doesn't allow special characters. It only allows alphanumeric with first letter a char. So u can't have {,},etc.. It follows the same naming convention as variables.

0

精彩评论

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