Function
其中占位符T(type)代表入参类型,R(return)代表返回类型
Functiontimes2 = e -> e * 2; Function squared = e -> e * e; //将参数代入(10*2=20) Integer apply2 = times2.apply(10); //在times2函数执行之前先执行squared函数 (先运行 4*4=16 再 16*2=32) Integer apply = times2.compose(squared).apply(4); //在times2执行之后,再执行squared函数 (先运行4*2=8 再8*8=64) Integer apply1 = times2.andThen(squared).apply(4); //直接返回传入的参数 Function fun1 = Function.identity(); String str1 = fun1.apply("helloWorld"); System.out.println("apply == " + apply); //apply == 32 System.out.println("apply1 == " + apply1); //apply1 == 64 System.out.println("apply2 == " + apply2); //apply2 == 20 System.out.println(str1); //helloWorld
- apply方法是将入参带入方法中
- compose()括号中的内容在调用函数time2之前运行 4*4=16 16*2 =32
- andThen()括号中的内容在调用函数time2结束之后运行 4*2 =8 8*8=64
- identity() 方法直接返回传入的参数



