Java 枚举映射到单个方法的多个键

Java Enum Multiple Keys mapped to single method

我正在查看来自 here 的代码:

enum Operator {
    ADD {
        @Override int execute(final int num1, final int num2) {
            return num1 + num2;
        }
    },
    SUBTRACT {
        @Override int execute(final int num1, final int num2) {
            return num1 - num2;
        }
    },
    MULTIPLY {
        @Override int execute(final int num1, final int num2) {
            return num1 * num2;
        }
    },
    DIVIDE {
        @Override int execute(final int num1, final int num2) {
            if (num2 != 0) {
                return num1 / num2;
            } else {
                System.out.println("Can't divide by zero.");
            }
            return 0;
        }
    };

    abstract int execute(int num1, int num2);
}

public class EnumWithoutDefinedFunctions {
    public static void main(String[] args) {
        int num1 = 10;
        int num2 = 2;
        Operator operator = Operator.DIVIDE;
        int result = operator.execute(num1, num2);
        System.out.println("result: " + result);
    }
}

我正在寻找一种使用不同键调用相同方法的方法。例如,ADDADD2 应该执行相同的实现

ADD,ADD2 {
    @Override int execute(final int num1, final int num2) {
        return num1 + num2;
    }
},

但是,上述代码更改导致以下错误:

我找到了一个解决方法,不确定这是否是优化的解决方案,即创建一个方法,然后为需要相同实现的键调用相同的方法:

public class EnumTest {
    enum Operator {
        ADD {
            @Override int execute(final int num1, final int num2) {
                return addImpl( num1,num2);
            }
        },
        ADD2 {
            @Override int execute(final int num1, final int num2) {
                return addImpl( num1,num2);
            }
        },
        SUBTRACT {
            @Override int execute(final int num1, final int num2) {
                return num1 - num2;
            }
        },
        MULTIPLY {
            @Override int execute(final int num1, final int num2) {
                return num1 * num2;
            }
        },
        DIVIDE {
            @Override int execute(final int num1, final int num2) {
                if (num2 != 0) {
                    return num1 / num2;
                } else {
                    System.out.println("Can't divide by zero.");
                }
                return 0;
            }
        };

        private static int addImpl(int num1, int num2){
            return num1 + num2;
        }

        abstract int execute(int num1, int num2);
    }

    public static void main(String[] args) {
        int num1 = 10;
        int num2 = 2;
        Operator operator = Operator.ADD2;
        int result = operator.execute(num1, num2);
        System.out.println("result: " + result);
    }
}

IMO 您可以使用功能接口改进设计 BiFunction

代码如下所示:

public class EnumTest {

    enum Operator implements BiFunction<Integer, Integer, Integer> {
        ADD(Operator::addImpl),
        ADD2 (Operator::addImpl),
        SUBTRACT((num1, num2) -> num1 - num2),
        MULTIPLY((num1, num2) -> num1 * num2),
        DIVIDE (Operator::divide);

        public static Integer divide(Integer num1, Integer num2) {
            if (num2 != 0) {
                return num1 / num2;
            } else {
                System.out.println("Can't divide by zero.");
            }
            return 0;
        }

       final BiFunction<Integer, Integer, Integer> biFunction;

       Operator(BiFunction<Integer, Integer, Integer> apply){
           this.biFunction = apply;
       }

       public Integer apply(Integer a, Integer b){
           return biFunction.apply(a, b);
       }

        private static int addImpl(int num1, int num2){
            return num1 + num2;
        }
    }
    public static void main(String[] args) {
        int num1 = 10;
        int num2 = 2;
        Operator operator = Operator.ADD2;
        int result = operator.apply(num1, num2);
        System.out.println("result: " + result);
    }
}

并且可以利用 BiFunction 接口很好地组合多个操作,例如:

Function<Integer, Integer> multiplyBy2 = l -> 2 * l;
int result2 = Operator.ADD2.andThen(multiplyBy2).apply(num1, num2);
System.out.println(result2);

输出:

24