package com.djh.exec;
/**
*
* 枚举定义
*
*/
enum Num{
ONE(100,"one"),TWO(200,"two"),THREE(300,"three"),FOUR(400,"four"),OTHER(900,"错误");
private int n ;
private String value;
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
private Num(int n,String value) {
this.n=n;
this.value=value;
}
public static Num getNum(int n ,String v) {
for(Num num: Num.values()) {
int n1 = num.getN();
String v1 = num.getValue();
if(n1==n && v.equals(v1)) {
return num;
}
}
return Num.OTHER;
}
}
/**
*
* 枚举的使用
*
*/
public class Exe {
public static void main(String[] args) {
Num n = Num.getNum(100, "one");
switch(n) {
case ONE:
System.out.println(Num.ONE);
break;
case TWO:
System.out.println(Num.TWO);
break;
case THREE:
System.out.println(Num.THREE);
break;
case FOUR:
System.out.println(Num.FOUR);
break;
default:
System.out.println(n.toString());
}
}
}
Java枚举的使用案例
最新推荐文章于 2022-01-20 20:00:00 发布