java,我想使用枚舉實(shí)現(xiàn)int到string的轉(zhuǎn)換,能做到嗎?就如同字典一樣。
擁有18年軟件開發(fā)和IT教學(xué)經(jīng)驗(yàn)。曾任多家上市公司技術(shù)總監(jiān)、架構(gòu)師、項(xiàng)目經(jīng)理、高級軟件工程師等職務(wù)。 網(wǎng)絡(luò)人氣名人講師,...
public enum MyDict {
ChineseEnglish(0, "漢語詞典"),
EnglishChinese(1,"英漢詞典"),
EnglishEnglish(2,"英英詞典");
Integer id;
String desc;
MyDict(Integer id, String desc) {
this.id = id;
this.desc = desc;
}
static MyDict findById(Integer id) {
MyDict dict;
switch(id) {
case 0:
dict = MyDict.ChineseEnglish;
break;
case 1:
dict = MyDict.EnglishChinese;
break;
case 2:
dict = MyDict.EnglishEnglish;
break;
default:
throw new IllegalArgumentException("非法ID");
}
return dict;
}
String getDesc() {
return desc;
}
public static void main(String[] args) {
String desc = MyDict.findById(0).getDesc();
System.out.println(desc);
}
}
I don’t know if that’s what it means