java enum 枚举类型用例

enum 很少用到,之前只是简单的应用,跟同事学到一招,记录下来



public enum EnumDemo2 {
everyday("棕色", "中号", "可调节"), special("黑色", "小号", "单肩");


private String color;


private String getColor() {
return this.color;
}


private String size;


private String getSize() {
return this.size;
}


private String way;


private String getWay() {
return this.way;
}


private EnumDemo2(String str1, String str2, String str3) {
this.color = str1;
this.size = str2;
this.way = str3;
}


public static void main(String[] args) {
for (EnumDemo2 demo : EnumDemo2.values()) {
System.out.println(demo + " : " + demo.getColor() + ","
+ demo.getSize() + "," + demo.getWay());
switch (demo) {
case everyday:
System.out.println("0");
break;
case special:
System.out.println("1");
break;
default:
System.out.println("-100");
break;
}
}


// for (EnumDemo2 demo : EnumDemo2.values()) {
// System.out.println(demo.name() + "," + demo.ordinal() + ","
// + demo.getColor() + "," + demo.getSize() + ","
// + demo.getWay());
// }


System.out.println(EnumDemo2.valueOf("everyday").getColor());


// System.out.println(EnumDemo2.everyday.getDeclaringClass());
}
}

你可能感兴趣的:(java)