| 知乎专栏 |
package cn.netkiller.test;
import java.util.function.Function;
enum Operation {
ADD(x -> x + 10), MULTIPLY(x -> x * 2), SQUARE(x -> x * x);
private final Function<Integer, Integer> func;
Operation(Function<Integer, Integer> func) {
this.func = func;
}
public int apply(int value) {
return func.apply(value);
}
}
public class Test {
static void main(String[] args) {
// 输出 10
int result = Operation.MULTIPLY.apply(5);
System.out.println(result);
}
}