import com.example.javaone.kk.MyEnum;
public class Gst {
public static void main(String[] args) {
MyEnum e=MyEnum.getById(1);
System.out.println(e.getGetSize());
}
}
package com.example.javaone.kk;
public enum MyEnum {
ENUM1(1,2),
ENUM2(2,3),
ENUM3(3,4);
private int id;
public int getGetSize() {
return getSize;
}
private int getSize;
MyEnum(int id,int getSize) {
this.id = id;
this.getSize = getSize;
}
public int getId() {
return id;
}
public static MyEnum getById(int id) {
for (MyEnum enumValue : MyEnum.values()) {
if (enumValue.getId() == id) {
return enumValue;
}
}
throw new IllegalArgumentException("Invalid ID: " + id);
}
}
该代码可以解决多个if-else 而且可以解决多个模块使用多个if-else