zoukankan      html  css  js  c++  java
  • lambda表达式

    心血来潮,突然想看看,于是百度了一下。

    稍作总结:用作实现匿名内部类的简化语法。

    摘抄一段代码,比什么解释都好用,感谢runnoob:

    http://www.runoob.com/java/java8-lambda-expressions.html

    Java8Tester.java 文件
    public class Java8Tester {
       public static void main(String args[]){
          Java8Tester tester = new Java8Tester();
            
          // 类型声明
          MathOperation addition = (int a, int b) -> a + b;
            
          // 不用类型声明
          MathOperation subtraction = (a, b) -> a - b;
            
          // 大括号中的返回语句
          MathOperation multiplication = (int a, int b) -> { return a * b; };
            
          // 没有大括号及返回语句
          MathOperation division = (int a, int b) -> a / b;
            
          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));
            
          // 不用括号
          GreetingService greetService1 = message ->
          System.out.println("Hello " + message);
            
          // 用括号
          GreetingService greetService2 = (message) ->
          System.out.println("Hello " + message);
            
          greetService1.sayMessage("Runoob");
          greetService2.sayMessage("Google");
       }
        
       interface MathOperation {
          int operation(int a, int b);
       }
        
       interface GreetingService {
          void sayMessage(String message);
       }
        
       private int operate(int a, int b, MathOperation mathOperation){
          return mathOperation.operation(a, b);
       }
    }
    


  • 相关阅读:
    php CI笔记
    Apache 2.4权限设置( you don't have permission to access / on this server Apache2.4)
    关闭浏览器时退出登录
    onunload 和 onbeforeunload都不执行
    apache ab压力测试工具需要用户登录才能测得时候怎么办?
    《国富论》读书笔记
    数据库设计技巧
    溜到不行。。
    Session和Cookie
    c#缓存
  • 原文地址:https://www.cnblogs.com/the-fool/p/11054191.html
Copyright © 2011-2022 走看看