您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)java8中java.util.function.*pojo反射的示例分析,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
寫一個(gè)普通的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; } }
傳統(tǒng)的方式
// 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訪問(wèn)方式
// 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訪問(wèn)方式
// 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");
訪問(wèn) constructor 創(chuàng)建新實(shí)例
// 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");
帶參數(shù)的構(gòu)建器
// 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");
關(guān)于“java8中java.util.function.*pojo反射的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。