002.Lambda举例(只能有一个函数式接口时使用)
package com.imooc.lambda; /** * 四则运算接口 */ public interface MathOpertion { public Float operate(Integer a,Integer b); }
package com.imooc.lambda; public class LambdaSample { public static void main(String[] args) { //1.标准使用 //约束条件:Lambda只能实现有且仅有一个抽象方法的接口,Java称为“函数式接口” MathOpertion addition = (Integer a, Integer b) -> { System.out.println("加法运算"); return a + b + 0f; }; System.out.println(addition.operate(3, 50)); //2.Lambda允许忽略参数类型 MathOpertion substraction = (a, b) -> { System.out.println("减法运算"); return a - b + 0f; }; System.out.println(substraction.operate(9, 1)); //3.单行实现代码可以省略大括号和return MathOpertion multiplication = (a,b) -> a*b+0f; System.out.println("乘法运算"); System.out.println(multiplication.operate(9,8)); } }