Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

12.9. Enum + Function

		
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);
    }

}