91aaa在线国内观看,亚洲AV午夜福利精品一区二区,久久偷拍人视频,久久播这里有免费视播

<strong id="fvuar"></strong>

  • <sub id="fvuar"><dl id="fvuar"><em id="fvuar"></em></dl></sub>

    1. 千鋒教育-做有情懷、有良心、有品質的職業(yè)教育機構

      手機站
      千鋒教育

      千鋒學習站 | 隨時隨地免費學

      千鋒教育

      掃一掃進入千鋒手機站

      領取全套視頻
      千鋒教育

      關注千鋒學習站小程序
      隨時隨地免費學習課程

      當前位置:首頁  >  技術干貨  > java方法引用是什么

      java方法引用是什么

      來源:千鋒教育
      發(fā)布人:qyf
      時間: 2022-08-10 15:17:36 1660115856

      java方法引用是什么

        1、說明

        方法引用可以看作是Lambda表達式的深層表達。換句話說,方法引用是Lambda表達式,也就是函數接口的例子,通過方法名稱指向方法。

        2、使用場景

        當要傳遞給 Lambda 體的操作,已經實現的方法了,可以使用方法引用

        3、格式

        類(或對象) :: 方法名

        4、實例

        public class MethodRefTest {

        // 情況一:對象 :: 實例方法

        //Consumer中的void accept(T t)

        //PrintStream中的void println(T t)

        @Test

        public void test1() {

        //使用Lambda表達

        Consumercon1 = str -> System.out.println(str);

        con1.accept("中國");

        System.out.println("====================");

        //使用方法引用

        PrintStream ps = System.out;

        Consumer con2 = ps::println;

        con2.accept("China");

        }

        //Supplier中的T get()

        //Employee中的String getName()

        @Test

        public void test2() {

        //使用Lambda表達

        Employee emp = new Employee(1001, "Bruce", 34, 600);

        Suppliersup1 = () -> emp.getName();

        System.out.println(sup1.get());

        System.out.println("====================");

        //使用方法引用

        Supplier sup2 = emp::getName;

        System.out.println(sup2.get());

        }

        // 情況二:類 :: 靜態(tài)方法

        //Comparator中的int compare(T t1,T t2)

        //Integer中的int compare(T t1,T t2)

        @Test

        public void test3() {

        //使用Lambda表達

        Comparatorcom1 = (t1, t2) -> Integer.compare(t1, t2);

        System.out.println(com1.compare(32, 45));

        System.out.println("====================");

        //使用方法引用

        Comparatorcom2 = Integer::compareTo;

        System.out.println(com2.compare(43, 34));

        }

        //Function中的R apply(T t)

        //Math中的Long round(Double d)

        @Test

        public void test4() {

        //使用匿名內部類

        Function<double, long=""> func = new Function<double, long="">() {

        @Override

        public Long apply(Double aDouble) {

        return Math.round(aDouble);

        }

        };

        System.out.println(func.apply(10.5));

        System.out.println("====================");

        //使用Lambda表達式

        Function<double, long=""> func1 = d -> Math.round(d);

        System.out.println(func1.apply(12.3));

        System.out.println("====================");

        //使用方法引用

        Function<double, long=""> func2 = Math::round;

        System.out.println(func2.apply(12.6));

        }

        // 情況三:類 :: 實例方法

        // Comparator中的int comapre(T t1,T t2)

        // String中的int t1.compareTo(t2)

        @Test

        public void test5() {

        //使用Lambda表達式

        Comparatorcom1 = (s1, s2) -> s1.compareTo(s2);

        System.out.println(com1.compare("abd", "aba"));

        System.out.println("====================");

        //使用方法引用

        Comparatorcom2 = String::compareTo;

        System.out.println(com2.compare("abd", "abc"));

        }

        //BiPredicate中的boolean test(T t1, T t2);

        //String中的boolean t1.equals(t2)

        @Test

        public void test6() {

        //使用Lambda表達式

        BiPredicate<string, string=""> pre1 = (s1, s2) -> s1.equals(s2);

        System.out.println(pre1.test("abc", "abc"));

        System.out.println("====================");

        //使用方法引用

        BiPredicate<string, string=""> pre2 = String::equals;

        System.out.println(pre2.test("abc", "abd"));

        }

        // Function中的R apply(T t)

        // Employee中的String getName();

        @Test

        public void test7() {

        //使用Lambda表達式

        Employee employee = new Employee(1001, "Tom", 45, 10000);

        Function<employee, string=""> func1 =e->e.getName();

        System.out.println(func1.apply(employee));

        System.out.println("====================");

        //使用方法引用

        Function<employee,string>func2 = Employee::getName;

        System.out.println(func2.apply(employee));

        }

        }

        以上就是java方法引用的介紹,希望對大家有所幫助。更多關于“java培訓”的問題,歡迎咨詢千鋒教育在線名師。千鋒教育多年辦學,課程大綱緊跟企業(yè)需求,更科學更嚴謹,每年培養(yǎng)泛IT人才近2萬人。不論你是零基礎還是想提升,都可以找到適合的班型,千鋒教育隨時歡迎你來試聽。

      tags:
      聲明:本站稿件版權均屬千鋒教育所有,未經許可不得擅自轉載。
      10年以上業(yè)內強師集結,手把手帶你蛻變精英
      請您保持通訊暢通,專屬學習老師24小時內將與您1V1溝通
      免費領取
      今日已有369人領取成功
      劉同學 138****2860 剛剛成功領取
      王同學 131****2015 剛剛成功領取
      張同學 133****4652 剛剛成功領取
      李同學 135****8607 剛剛成功領取
      楊同學 132****5667 剛剛成功領取
      岳同學 134****6652 剛剛成功領取
      梁同學 157****2950 剛剛成功領取
      劉同學 189****1015 剛剛成功領取
      張同學 155****4678 剛剛成功領取
      鄒同學 139****2907 剛剛成功領取
      董同學 138****2867 剛剛成功領取
      周同學 136****3602 剛剛成功領取
      相關推薦HOT