在java中使用枚举类,并使用switch语句
public class EnumTest {
public static void main(String[] args) {
System.out.println("compony give me a phone,what brand is?");
String phoneBrand = "08";
switch (CellphoneBrandEnum.getByValue(phoneBrand)) {
case Apple:
System.out.println("I hate Apple");
break;
case Samsung:
System.out.println("I hate Samsung");
break;
case Huawei:
System.out.println("I hate Huawei");
break;
case Xiaomi:
System.out.println("I hate Xiaomi");
break;
case Oppo:
System.out.println("I hate Oppo");
break;
case Vivo:
System.out.println("I hate Vivo");
break;
default:
System.out.println("I hate you");
break;
}
}
}
public enum CellphoneBrandEnum {
Apple("01"), Samsung("02"), Huawei("03"), Xiaomi("04"), Oppo("05"), Vivo("06"), Unknow("");
String brandName;
CellphoneBrandEnum(String name) {
brandName = name;
}
String getValue() {
return brandName;
}
public static CellphoneBrandEnum getByValue(String value) {
for (CellphoneBrandEnum e : values()) {
if (e.getValue().equals(value)) {
return e;
}
}
return CellphoneBrandEnum.Unknow;
}
}
如果有不对的,请各位多多指正,谢谢。