java三板斧_Java 枚举使用三板斧
Java 枚舉使用三板斧
1 定義
public enum CountryEnums {
ONE(1,"韓"),TWO(2,"魏"),THREE(3,"楚"),FOUR(4,"燕"),FIVE(5,"趙"),SIX(6,"齊");
private Integer retCode;
private String retMsg;
// 枚舉的構(gòu)造方法是 private 的
private CountryEnums(Integer retCode,String retMsg){
this.retCode = retCode;
this.retMsg = retMsg;
}
public Integer getRetCode() {
return retCode;
}
public void setRetCode(Integer retCode) {
this.retCode = retCode;
}
public String getRetMsg() {
return retMsg;
}
public void setRetMsg(String retMsg) {
this.retMsg = retMsg;
}
2 遍歷
// 枚舉的遍歷
public static CountryEnums forEachCountryEnums(Integer index){
for(CountryEnums element : values()){
if(element.getRetCode() == index){
return element;
}
}
return null;
}
}
3 使用
for(int i = 1; i <= 6; i++){
System.out.println(CountryEnums.forEachCountryEnums(i).getRetMsg());
}
原文:https://www.cnblogs.com/karong007/p/11230029.html
總結(jié)
以上是生活随笔為你收集整理的java三板斧_Java 枚举使用三板斧的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 甘蓝多少钱啊?
- 下一篇: 渭南治疗少精弱精最好的医院推荐