Lambda且接口


interface MathOperation{
	int operation(int a,int b);
}

private int operation(int a,int b,MathOperation mathOperation){
	return mathOperation.operation(a,b);
}

MathOperation addition = (a, b) -> a + b;
MathOperation subtraction = (a, b) -> a - b;
MathOperation multiplication = (a, b) -> a * b;
MathOperation division = (a, b) -> a / b;


Java8Tester tester = new Java8Tester();
System.out.println("10 + 5 = " + tester.operate(10, 5, addition));
System.out.println("10 - 5 = " + tester.operate(10, 5, subtraction));
System.out.println("10 x 5 = " + tester.operate(10, 5, multiplication));
System.out.println("10 / 5 = " + tester.operate(10, 5, division));

猜你喜欢

转载自1971161579.iteye.com/blog/2356854