理解java8中java.util.function.*pojo反射新方法(附代码)
之前的文章《浅析EOS区块链柚子钱包前端插件scatter开发(分享)》中,给大家了解了区块链中EOS钱包前端插件scatter开发。下面本篇文章给大家了解java.util.function.*pojo反射新方法,伙伴们来看看一下。
上代码,看例子
写一个普通的POJO
public class City { private String name; private String code; public City() { } public City(String name, String code) { this.name = name; this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } }
传统的方式
// Use a constructor with parameters to create a City City sf = new City("San Francisco", "SF"); // Use a default constructor with no parameters to create a City City la = new City(); // Set the members using setters la.setName("Los Angeles"); la.setCode("LA");
新的getter访问方式
// Use the City's method references and assign them to functions Function<City, String> getNameFunction = City::getName; Function<City, String> getCodeFunction = City::getCode; System.out.println("The code for " + getNameFunction.apply(sf) + " is " + getCodeFunction.apply(sf)); -> The code for San Francisco is SF
新的setter访问方式
// Use the City's method references and assign them to biconsumers BiConsumer<City, String> setNameBiConsumer = City::setName; BiConsumer<City, String> setCodeBiConsumer = City::setCode; City ny = new City(); setNameBiConsumer.accept(ny, "New York"); setCodeBiConsumer.accept(ny, "NY");
访问 constructor 创建新实例
// Use the City's constructor method reference to create // a default constructor reference. Supplier<City> defaultConstructor = City::new; City sd = defaultConstructor.get(); sd.setName("San Diego"); sd.setCode("SD");
带参数的构建器
// Use the City's constructor method reference to create // a two-parameter constructor reference. BiFunction<String, String, City> twoParameterConstructor = City::new; City dc = twoParameterConstructor.apply("Washington, D. C.", "DC");
推荐学习:java视频教程
以上是理解java8中java.util.function.*pojo反射新方法(附代码)的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

Java8计算一年前或一年后的日期利用minus()方法计算一年前的日期packagecom.shxt.demo02;importjava.time.LocalDate;importjava.time.temporal.ChronoUnit;publicclassDemo09{publicstaticvoidmain(String[]args){LocalDatetoday=LocalDate.now();LocalDatepreviousYear=today.minus(1,ChronoUni

Java8如何计算一周后的日期这个例子会计算一周后的日期。LocalDate日期不包含时间信息,它的plus()方法用来增加天、周、月,ChronoUnit类声明了这些时间单位。由于LocalDate也是不变类型,返回后一定要用变量赋值。packagecom.shxt.demo02;importjava.time.LocalDate;importjava.time.temporal.ChronoUnit;publicclassDemo08{publicstaticvoidmain(String[

Java8的Clock时钟类Java8增加了一个Clock时钟类用于获取当时的时间戳,或当前时区下的日期时间信息。以前用到System.currentTimeInMillis()和TimeZone.getDefault()的地方都可用Clock替换。packagecom.shxt.demo02;importjava.time.Clock;publicclassDemo10{publicstaticvoidmain(String[]args){//Returnsthecurrenttimebase

在Java8中获取当前的时间戳Instant类有一个静态工厂方法now()会返回当前的时间戳,如下所示:packagecom.shxt.demo02;importjava.time.Instant;publicclassDemo16{publicstaticvoidmain(String[]args){Instanttimestamp=Instant.now();System.out.println("Whatisvalueofthisinstant"+timestamp.t

Java8中如何使用预定义的格式化工具去解析或格式化日期packagecom.shxt.demo02;importjava.time.LocalDate;importjava.time.format.DateTimeFormatter;publicclassDemo17{publicstaticvoidmain(String[]args){StringdayAfterTommorrow="20180205";LocalDateformatted=LocalDate.parse

Java8中处理时区Java8不仅分离了日期和时间,也把时区分离出来了。现在有一系列单独的类如ZoneId来处理特定时区,ZoneDateTime类来表示某时区下的时间。这在Java8以前都是GregorianCalendar类来做的。下面这个例子展示了如何把本时区的时间转换成另一个时区的时间。packagecom.shxt.demo02;importjava.time.LocalDateTime;importjava.time.ZoneId;importjava.time.ZonedDateT

Java8中获取今天的日期Java8中的LocalDate用于表示当天日期。和java.util.Date不同,它只有日期,不包含时间。当你仅需要表示日期时就用这个类。packagecom.shxt.demo02;importjava.time.LocalDate;publicclassDemo01{publicstaticvoidmain(String[]args){LocalDatetoday=LocalDate.now();System.out.println("今天的日期:&q

Java8中判断两个日期是否相等packagecom.shxt.demo02;importjava.time.LocalDate;publicclassDemo04{publicstaticvoidmain(String[]args){LocalDatedate1=LocalDate.now();LocalDatedate2=LocalDate.of(2018,2,5);if(date1.equals(date2)){System.out.println("时间相等");}e
