package com.richard.lambdaexpressions;
public class Calculator {
interface IntegerMath{
int operation(int a,int b);
}
public int operateBinary(int a,int b,IntegerMath op){
return op.operation(a, b);
}
public static void main(String[] args){
Calculator myApp=new Calculator();
IntegerMath addtion=(a,b)->a+b;
IntegerMath subtraction=(a,b)->a-b;
System.out.println("40+2= "+myApp.operateBinary(40,2, addtion));
System.out.println("50-20= "+myApp.operateBinary(50, 20, subtraction));
System.out.println("*********************");
int result=myApp.operateBinary(120, 110, new IntegerMath(){
@Override
public int operation(int a, int b) {
// TODO Auto-generated method stub
return (a+b);
}
});
System.out.println(result);
System.out.println("*********************");
int result1=myApp.operateBinary(100, 200, new IntegerMath(){
public int operation(int a,int b){
return (a-b);
}
});
System.out.println(result1);
}
}
网友评论