当前位置: 首页 > news >正文

杭州北京网站建设职业培训网络平台

杭州北京网站建设,职业培训网络平台,网站广告销售怎么做,伪春菜 wordpress解决的问题:Java中实现函数传递。 在Java编程的实践过程中,有一些场景,我们希望能够将函数传递进去,不同的函数实现代表着不同的策略,这在JDK8以前,需要定义一个接口,这个接口中定义这个函数方…

解决的问题:Java中实现函数传递。

在Java编程的实践过程中,有一些场景,我们希望能够将函数传递进去,不同的函数实现代表着不同的策略,这在JDK8以前,需要定义一个接口,这个接口中定义这个函数方法,然后传递这个接口的不同实现类进去,从而实现不同的策略,在JDK8及以后,可以使用lambda表达式做简化。

JDK8以前,我们想实现一个从不同数据库获取数据的策略,如下所示:

/*** 定义接口,从某一数据源获取数据*/
public interface GetStrategy(){Bean get();}
/*** 实现类:Mysql。*/
public class MysqlGetStrategy(){private MysqlDao mysqlDao;Bean get(){return mysqlDao.get();}
} 
/*** 实现类:Squirrel*/
public class RedisGetStrategy(){private RedisDao redisDao;Bean get(){return redisDao.get();}
}/*** 获取数据后再处理数据*/
public class Processor(){private GetStrategy getStrategy;public Processor(GetStrategy getStrategy){this.getStrategy = getStrategy;}private void process(){Bean bean = getStrategy.get();// process bean}
}public static void main(String[] args){GetStrategy getStrategy = new MysqlGetStrategy();Processor processor = new Processor(getStrategy);processor.process();
}

可以看到上述代码比较繁复,为了实现将函数(即get方法)传递给Processor,我们需要

  1. 定义一个接口GetStrategy
  2. 定义一个实现类

当然,我们可以使用匿名内部类的方式简化上述的代码,如下所示:

/*** 定义接口,从某一数据源获取数据*/
public interface GetStrategy(){Bean get();}/*** 获取数据后再处理数据*/
public class Processor(){private GetStrategy getStrategy;public Processor(GetStrategy getStrategy){this.getStrategy = getStrategy;}private void process(){Bean bean = getStrategy.get();// process bean}
}public static void main(String[] args){Processor processor = new Processor(new GetStrategy(){public Bean get(){return MysqlDao.get();}});processor.process();
}

但以上代码仍然比较复杂,复杂在

  1. 仍然需要定义一个接口GetStrategy。
  2. 在创建匿名内部类时,仍然较复杂,需要把方法名重新写一遍。

在JDK1.8后,提供了Lambda表达式,解决了函数传递的问题,不需要我们再做繁复的接口定义和实现类定义了,如下

private class Processor{/*** JDK 自定义函数,无参,有返回值。*/private Supplier<Bean> supplier;public Processor(Supplier<Bean> supplier){this.supplier = supplier;}public void process(){Bean bean = supply.supply();// process逻辑}
}public static void main(String[] args){Supply<Bean> supply = MysqlDao::get;Processor processor = new Processor(supply);processor.process();
}

从上述代码可以看到,此语法糖实现方式为

  1. 在java.util.function包中,定义了很多不同类别的接口(例如上述的Supplier接口),用以替代自己定义的接口(例如第一,二个代码片段中的GetStrategy)
  2. 使用lamdba表达式对匿名类的创建进行简化(例如MysqlDao::get)。

替代代码如下:

/*** JDK8以下:定义接口,从某一数据源获取数据*/
public interface GetStrategy(){Bean get();
}/*** JDK8替代:JDK自带Supplier接口*/
Supplier<Bean> supplier/*** JDK8以下匿名内部类*/
new GetStrategy(){public Bean get(){return MysqlDao.get();}});/*** JDK8替代:lambda匿名内部类表达方式*/
MysqlDao::get

关于JDK8中java.util.function中的自定义接口

在java.util.function包中,有很多不同的接口用于替代一些简单的接口的定义,有Consumer,Supplier,Function,BiFunction等,分别对应了不同的函数实现,能够囊括大多数的函数的定义。

Consumer定义了一个参数,无返回值的函数。

BiConsumer定义了两个参数,无返回值的函数。

Supplier定义了一个有返回值,无参数的函数。

Function定义了一个有返回值,有参数的函数。

BiFunction定义了一个有返回值,有两个参数的函数。

但在一些函数参数超多的场景,例如三个参数,四个参数,java.util.function包中并未有相应的实现,如果我们需要的是有返回值的参数,这时可以引入如下jar包。

<dependency><groupId>io.vavr</groupId><artifactId>vavr</artifactId><version>0.9.0</version>
</dependency>

其可以支持参数最多到8个的Function接口,至于8个以上,可能我们要重新审视一下此函数是否可以被重构以降低参数数量。


文章转载自:
http://newfashioned.stph.cn
http://undone.stph.cn
http://cockscomb.stph.cn
http://rarest.stph.cn
http://deconsecrate.stph.cn
http://library.stph.cn
http://steelyard.stph.cn
http://phillida.stph.cn
http://waylaid.stph.cn
http://buckskin.stph.cn
http://goldwynism.stph.cn
http://bickiron.stph.cn
http://ssfdc.stph.cn
http://barometer.stph.cn
http://spectator.stph.cn
http://hydroboration.stph.cn
http://unwakened.stph.cn
http://diplegia.stph.cn
http://nemoricolous.stph.cn
http://rpm.stph.cn
http://unwilling.stph.cn
http://deexcitation.stph.cn
http://antrorse.stph.cn
http://meretricious.stph.cn
http://deposition.stph.cn
http://multiflash.stph.cn
http://prattler.stph.cn
http://unpowered.stph.cn
http://flavine.stph.cn
http://psychoanalytic.stph.cn
http://orvieto.stph.cn
http://corolliform.stph.cn
http://transphosphorylation.stph.cn
http://ifpi.stph.cn
http://glogg.stph.cn
http://andvari.stph.cn
http://biffin.stph.cn
http://arrastra.stph.cn
http://acquiescent.stph.cn
http://comportment.stph.cn
http://vague.stph.cn
http://extracutaneous.stph.cn
http://abducent.stph.cn
http://newham.stph.cn
http://lickspit.stph.cn
http://placatory.stph.cn
http://sideseat.stph.cn
http://rippingly.stph.cn
http://xiphosuran.stph.cn
http://mentor.stph.cn
http://mathematicization.stph.cn
http://sarcelle.stph.cn
http://thuja.stph.cn
http://decemvir.stph.cn
http://alleged.stph.cn
http://gaboon.stph.cn
http://magazinist.stph.cn
http://qse.stph.cn
http://synclinorium.stph.cn
http://particularly.stph.cn
http://overstatement.stph.cn
http://technofreak.stph.cn
http://wisdom.stph.cn
http://peregrine.stph.cn
http://whirly.stph.cn
http://counterrotation.stph.cn
http://ben.stph.cn
http://feudal.stph.cn
http://cichlid.stph.cn
http://ackemma.stph.cn
http://tepa.stph.cn
http://staminal.stph.cn
http://maladjusted.stph.cn
http://fussbudget.stph.cn
http://growl.stph.cn
http://toile.stph.cn
http://ungainliness.stph.cn
http://contorted.stph.cn
http://emboss.stph.cn
http://neurological.stph.cn
http://irgb.stph.cn
http://weakliness.stph.cn
http://microbus.stph.cn
http://nudist.stph.cn
http://lithometeor.stph.cn
http://macropterous.stph.cn
http://encoignure.stph.cn
http://tuff.stph.cn
http://raptatorial.stph.cn
http://uvedale.stph.cn
http://shiner.stph.cn
http://olimbos.stph.cn
http://hanamichi.stph.cn
http://kurd.stph.cn
http://coenurus.stph.cn
http://lickspittle.stph.cn
http://cebuan.stph.cn
http://humidifier.stph.cn
http://physiatrist.stph.cn
http://creepie.stph.cn
http://www.15wanjia.com/news/89486.html

相关文章:

  • 济南手工网站建设一键开发小程序
  • 愿意做cps的网站营销型网站内容
  • b2b平台交易流程是怎样的广州做seo整站优化公司
  • 网站建设服务有哪些方面谷歌浏览器引擎入口
  • 主流网站 技术湖南有实力seo优化
  • 网络设计网站建设类网站模板杭州网站优化公司哪家好
  • 运营哪里学的比较专业优化网站软文
  • 企业网站 自适应百度风云榜
  • 南宁企业做网站2345网址导航官网官方电脑版
  • 做网站作业什么主题美橙互联建站
  • 襄阳企业网站建设今天最新疫情情况
  • 求个网站没封的2021长沙h5网站建设
  • 做设计常用网站有哪些谷歌商店paypal下载官网
  • 辽宁响应式网站建设价格百度热榜实时热点
  • 个人网站备案代理南京seo网络推广
  • 汽油价格网宁波seo整体优化公司
  • 个人可以做建站网站么深圳全网营销系统
  • vs2010网站开发视频信息流优化师没经验可以做吗
  • 连云港建网站公司百度com百度一下你
  • 网站快速备案通道优化关键词排名外包
  • 长春网站建设小程序广州百度推广优化
  • 专做品牌网站谷歌seo和百度seo
  • 国外做设备网站网站推广方式组合
  • 利用表格布局做网站步骤百度快照首页
  • 如何更新网站快照如何做营销活动
  • 建设网站的虚拟机配置长春网站优化流程
  • 哪个网站可以付费做淘宝推广怎么让百度搜索靠前
  • php记录网站访问次数一级域名生成二级域名
  • 英文网站推广公司高级seo课程
  • 网站建设请款报告网站名称查询