JAVA/JAVA

enum 메소드 처리

lovineff 2020. 6. 4. 17:39
enum CalculatorTypeJava7{
    CALC_A{
        long calculate(long value){return value;}
    },
    CALC_B{
        long calculate(long value){return value * 10;}
    },
    CALC_C{
        long calculate(long value){return value - 10;}
    };

    abstract long calculate(long value);
}

enum CalculatorTypeJava8{
    CALC_A(value -> value),
    CALC_B(value -> value * 10),
    CALC_C(value -> value -10);

    private Function<Long, Long> expression;

    CalculatorTypeJava8(Function<Long, Long> expression){ this.expression = expression;}

    public long calculate(long value){return expression.apply(value);}
}

public class JavaTest {
    public static void main(String[] args) {
        // JAVA 7
        System.out.println(CalculatorTypeJava7.CALC_A.calculate(1000));
        System.out.println(CalculatorTypeJava7.CALC_B.calculate(1000));
        System.out.println(CalculatorTypeJava7.CALC_C.calculate(1000));

        // JAVA 8
        System.out.println(CalculatorTypeJava8.CALC_A.calculate(1000));
        System.out.println(CalculatorTypeJava8.CALC_B.calculate(1000));
        System.out.println(CalculatorTypeJava8.CALC_C.calculate(1000));
    }
}

 

참고.

http://woowabros.github.io/tools/2017/07/10/java-enum-uses.html