新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Java8方法引用與構(gòu)造器引用,數(shù)組引用舉例分析
這篇文章主要講解了“Java 8方法引用與構(gòu)造器引用,數(shù)組引用舉例分析”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Java 8方法引用與構(gòu)造器引用,數(shù)組引用舉例分析”吧!

成都網(wǎng)絡(luò)公司-成都網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)公司10多年經(jīng)驗成就非凡,專業(yè)從事做網(wǎng)站、成都網(wǎng)站設(shè)計,成都網(wǎng)頁設(shè)計,成都網(wǎng)頁制作,軟文發(fā)布平臺,一元廣告等。10多年來已成功提供全面的成都網(wǎng)站建設(shè)方案,打造行業(yè)特色的成都網(wǎng)站建設(shè)案例,建站熱線:028-86922220,我們期待您的來電!
import org.junit.Test;import java.io.PrintStream;import java.util.Comparator;import java.util.function.BiFunction;import java.util.function.BiPredicate;import java.util.function.Consumer;import java.util.function.Function;import java.util.function.Supplier;/* * 一、方法引用:若 Lambda 體中的功能,已經(jīng)有方法提供了實現(xiàn),可以使用方法引用 * (可以將方法引用理解為 Lambda 表達式的另外一種表現(xiàn)形式) *
* 1. 對象的引用 :: 實例方法名 *
* 2. 類名 :: 靜態(tài)方法名 *
* 3. 類名 :: 實例方法名 *
* 注意: * ①方法引用所引用的方法的參數(shù)列表與返回值類型,需要與函數(shù)式接口中抽象方法的參數(shù)列表和返回值類型保持一致! * ②若Lambda 的參數(shù)列表的第一個參數(shù),是實例方法的調(diào)用者,第二個參數(shù)(或無參)是實例方法的參數(shù)時,格式: ClassName::MethodName *
* 二、構(gòu)造器引用 :構(gòu)造器的參數(shù)列表,需要與函數(shù)式接口中參數(shù)列表保持一致! *
* 1. 類名 :: new *
* 三、數(shù)組引用 *
* 類型[] :: new; *
*
*/public class TestMethodRef { //數(shù)組引用 @Test public void test8(){
Function fun = (args) -> new String[args]; String[] strs = fun.apply(10); System.out.println(strs.length); System.out.println("--------------------------"); Function fun2 = Employee[] :: new; Employee[] emps = fun2.apply(20); System.out.println(emps.length); }
//構(gòu)造器引用 @Test public void test7(){
Function fun = Employee::new; Employee em=fun.apply("www"); System.out.println(em.getName()); BiFunction fun2 = Employee::new; }
@Test public void test6(){
Supplier sup = () -> new Employee(); System.out.println(sup.get()); System.out.println("------------------------------------"); Supplier sup2 = Employee::new; System.out.println(sup2.get()); }
//類名 :: 實例方法名 @Test public void test5(){
BiPredicate bp = (x, y) -> x.equals(y); System.out.println(bp.test("abcde", "abcde")); System.out.println("-----------------------------------------"); BiPredicate bp2 = String::equals; System.out.println(bp2.test("abc", "abc")); System.out.println("-----------------------------------------"); Function fun = (e) -> e.show(); System.out.println(fun.apply(new Employee())); System.out.println("-----------------------------------------"); Function fun2 = Employee::show; System.out.println(fun2.apply(new Employee())); }
//類名 :: 靜態(tài)方法名 @Test public void test4(){
Comparator com = (x, y) -> Integer.compare(x, y); System.out.println("-------------------------------------"); Comparator com2 = Integer::compare; }
@Test public void test3(){
BiFunction fun = (x, y) -> Math.max(x, y); System.out.println(fun.apply(1.5, 22.2)); System.out.println("--------------------------------------------------"); BiFunction fun2 = Math::max; System.out.println(fun2.apply(1.2, 1.5)); } //對象的引用 :: 實例方法名 @Test public void test2(){
Employee emp = new Employee(101, "張三", 18, 9999.99); Supplier sup = () -> emp.getName(); System.out.println(sup.get()); System.out.println("----------------------------------"); Supplier sup2 = emp::getName; System.out.println(sup2.get()); }
@Test public void test1(){
PrintStream ps = System.out; Consumer con = (str) -> ps.println(str); con.accept("Hello World!"); System.out.println("--------------------------------"); Consumer con2 = ps::println; con2.accept("Hello Java8!"); Consumer con3 = System.out::println; }
} 感謝各位的閱讀,以上就是“Java 8方法引用與構(gòu)造器引用,數(shù)組引用舉例分析”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對Java 8方法引用與構(gòu)造器引用,數(shù)組引用舉例分析這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
網(wǎng)站題目:Java8方法引用與構(gòu)造器引用,數(shù)組引用舉例分析
網(wǎng)頁路徑:http://www.dlmjj.cn/article/gcpssd.html


咨詢
建站咨詢
