2018-02-07

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

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • package com.richard.lambdaexpressions;import java.util.Li...
    hothome99阅读 180评论 0 1
  • New logo 创作你的创作 免费下载 以太坊学习 180 tenny1109 简书作者 2016.07.26 ...
    似曾相识2阅读 267评论 0 0
  • [TOC] 以下内容基于Android 8.0 Project Treble Project treble是And...
    Joe_HUST阅读 3,155评论 0 1
  • 从参加好报写作群到现在,咱也算是老资历了哈,一开始还会新鲜地把自己的文章投个首页什么的,记得就被推荐过一次首页,后...
    心眸阅读 290评论 6 2
  • Using OpenStack Identity Before working with the Identity...
    廖马儿阅读 463评论 0 0