父子枚举 二级枚举 枚举关联

来源:互联网 发布:windows 图标文件 编辑:程序博客网 时间:2024/06/07 02:07

两个相互关联的枚举,含有父子关系。

父枚举Type1:


public enum Type1 {
A("早餐",0),
B("午餐",1),
C("晚餐",2);
private Type1(String desc,int value){
this.desc = desc;
this.value = value;
}
private String desc;
private int value;

public String getDesc() {
return desc;
}


public int getValue() {
return value;
}

public static Type1 getName(int value) {
      for (Type1 c : Type1.values()) {
    if(c.getValue()==value){
    return c;
    }
      }
      return null;
    }
}

子枚举:



public enum Type2 {
a("馒头",0,0),
b("豆浆",1,0),
c("油条",2,0),
d("排骨",3,1),
e("牛奶",4,2);
private Type2(String desc,int value,int parent){
this.desc=desc;
this.value=value;
this.parent=parent;
}
private String desc;
private int value;
private int parent;



public String getDesc() {
return desc;
}


public int getValue() {
return value;
}


public int getParent() {
return parent;
}


    public static Type2 getName(int value,int parent) {
      for (Type2 c : Type2.values()) {
        if (c.getParent() == parent) {
        if(c.getValue()==value){
        return c;
        }
        }
      }
      return null;
    }
}

原创粉丝点击