Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of java.util.function.*pojo reflection in java8

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail about java.util.function.* in java 8 Pojo reflection example analysis, Xiaobian think quite practical, so share to everyone for a reference, I hope you can read this article after some harvest.

Write an ordinary 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; }}

a conventional manner

// Use a constructor with parameters to create a CityCity sf = new City("San Francisco", "SF");// Use a default constructor with no parameters to create a CityCity la = new City();// Set the members using settersla.setName("Los Angeles");la.setCode("LA");

New getter access method

// Use the City's method references and assign them to functionsFunction getNameFunction = City::getName;Function getCodeFunction = City::getCode;System.out.println("The code for " + getNameFunction.apply(sf) + " is " + getCodeFunction.apply(sf));-> The code for San Francisco is SF

New setter access method

// Use the City's method references and assign them to biconsumersBiConsumer setNameBiConsumer = City::setName;BiConsumer setCodeBiConsumer = City::setCode;City ny = new City();setNameBiConsumer.accept(ny, "New York");setCodeBiConsumer.accept(ny, "NY");

Visit constructor to create a new instance

// Use the City's constructor method reference to create// a default constructor reference.Supplier defaultConstructor = City::new;City sd = defaultConstructor.get();sd.setName("San Diego");sd.setCode("SD");

Parametric Builder

// Use the City's constructor method reference to create// a two-parameter constructor reference.BiFunction twoParameterConstructor = City::new;City dc = twoParameterConstructor.apply("Washington, D. C. ", "DC"); About" java.util.function.* in java8 Pojo reflection example analysis "This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report