根据枚举对应值来转化的技巧

根据枚举对应值来转化的技巧

常规

switch (x) {
    case "1":
        x = Globals.type.first.getvalue());
        break;
    case "2":
        x = Globals.type.second.getvalue());
        break;
    case "3":
        x = Globals.type.third.getvalue());
        break;
    default:

        break;
}
存在问题:如果有许多外面套层循环很多x需要改正,这样效率很低

解决方法:

for (Globals.type e : Globals.type.values()) {
    map.put(e.getCode(),e.getValue());
}
x=map.get(x);

猜你喜欢

转载自www.cnblogs.com/flyduckforever/p/11657758.html