美文网首页
2018-02-07

2018-02-07

作者: hothome99 | 来源:发表于2018-02-07 16:44 被阅读0次

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

    }

    相关文章

      网友评论

          本文标题:2018-02-07

          本文链接:https://www.haomeiwen.com/subject/jilazxtx.html