欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > Webflux必须要懂的函数式编程@FunctionalInterface

Webflux必须要懂的函数式编程@FunctionalInterface

2024/10/25 20:06:16 来源:https://blog.csdn.net/du_denglan/article/details/143236178  浏览:    关键词:Webflux必须要懂的函数式编程@FunctionalInterface

最近接手一个webflux 的项目,两眼一摸眼,Mono,Flux的两个手法,基本上全是函数式编程;虽然jdk1.8已经更新好多年了,但我最多用过JAVA 集合里的一些lamda表达式用了用函数,所以,难!

还是先写个DEMO,写上注解,理解一下

**計算器的函數編程demo **/
public class DemoFunctionInfTest {/**定义一个执行函数接口* 函数接口只能声明一个未实现的方法**/@FunctionalInterfaceinterface Calculate {/*** 执行方法* @param number1 参数1* @param number2 参数2* @return 返回值*/Integer execute(Integer number1,Integer number2);}//定义加法,并实现Calculate add=(a, b)->a+b;//定义乘法,并实现Calculate multi=(a, b)->a*b;//定义减法,方法注册Calculate sub;//定义除法,方法注册Calculate div;/*** 注册一个 除法函数* @param div* @return*/public DemoFunctionInfTest regDiv(Calculate div){this.div=div;return this;}/*** 註冊一個 減法函數* @param sub* @return*/public DemoFunctionInfTest regSub(Calculate sub){this.sub=sub;return this;}/*** 定义一个静态除法方法,用于注入函数式编程* @param a* @param b* @return*/public static Integer div(Integer a,Integer b){return a/b;}public static void main(String[] args) {//实例化对向 并注册减法(函数式注册),注册除法(静态方法注入)DemoFunctionInfTest demo=new DemoFunctionInfTest().regSub((a,b)->a-b).regDiv(DemoFunctionInfTest::div);//调用加减乘除System.out.println(demo.add.execute(10,20));System.out.println(demo.sub.execute(20,10));System.out.println(demo.multi.execute(10,20));System.out.println(demo.div.execute(20,10));}}

关键点:

1、函数编程必须只有一个函数定义(不包括default实现的函数)

2、实现函数方法有两种 1、lamda表达式 (这是函数入参的参数)->{实现体返回值为函数的返回}

                                       2、调用同等 入参,出参一致的 静态方法 传入: 类名::方法名

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com