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.
精彩评论