java固定键值转换,使用枚举实现字典?
本文介绍了java固定键值转换,使用枚举实现字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
java,我想使用枚举实现int到string的转换,能做到吗?就如同字典一样。
解决方案
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);
}
}
不知道是不是这个意思
这篇关于java固定键值转换,使用枚举实现字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文