zoukankan      html  css  js  c++  java
  • JAVA基础-枚举类型和Lamda表达式

    public class Test {
    public static void main(String args[]){
    Test tester = new Test();

      // 类型声明
      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("Baidu");
      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);
    }
    }

    // 输出:
    // 10 + 5 = 15
    // 10 - 5 = 5
    // 10 x 5 = 50
    // 10 / 5 = 2
    // Hello Baidu
    // Hello Google

  • 相关阅读:
    Oracle存储过程小记DUAL
    线程私有数据(TSD)
    Unix 五种基本I/O模型的区别
    Redis系列(0)应用场景
    linux ubuntu引导 win7
    Redis系列(一)启动流程分析
    c++ 内存管理小结
    设计模式Facade模式应用场景
    学会理财不做穷人
    jquery 注册验证例子
  • 原文地址:https://www.cnblogs.com/JeasonIsCoding/p/13232512.html
Copyright © 2011-2022 走看看