public enum ServiceType {
DEFAULT(-1) {
@Override
public IService getRegistService() {
return null;
}
},
APPSTORE(SystemConfig.ServiceTypeConfig.APPSTORE) {
@Override
public IService getRegistService() {
return new AppStoreService();
}
};
public abstract IService getRegistService();
public static ServiceType parseFrom(int value) {
if (map.containsKey(value)) {
return map.get(value);
}
return DEFAULT;
}
public static boolean check(int type) {
return (parseFrom(type) != DEFAULT);
}
public static String getRegistedServiceNames() {
StringBuilder sb = new StringBuilder();
for (ServiceType each : ServiceType.values()) {
if (each != DEFAULT) {
sb.append("类型[").append(each.getValue()).append("]:")
.append(each.getRegistService().getServiceName())
.append(" ");
}
}
return sb.toString();
}
private static final Map<Integer, ServiceType> map = new HashMap<Integer, ServiceType>();
static {
for (ServiceType each : ServiceType.values()) {
map.put(each.value, each);
}
}
private final int value;
public int getValue() {
return value;
}
private ServiceType(int value) {
this.value = value;
}
}
枚举类的应用,带抽象的方法
最新推荐文章于 2024-08-11 07:45:00 发布