java中的方法引用

來源:互聯網
上載者:User

標籤:val   轉換   function   new   類對象   hang   斷言   類型   int   

引用靜態方法:類名稱::static 方法名稱;

引用某個對象的方法:對象::普通方法;

引用特定類方法:特定類::方法

引用構造方法:類名稱::new

範例:引用靜態方法

package com.java.demo;interface IMessage<P,R>{ // P表示方法的參數類型,R表示方法的傳回值類型    public R change(P p);}public class TestDemo{    public static void main(String args[]){        /**         * 將基礎資料型別 (Elementary Data Type)轉換成String類型:public static String valueOf();         * 下面語句表示,將String類中改的valueOf()方法傳遞給change()方法         */        IMessage<Integer,String> msg = String :: valueOf;        String str = msg.change(100);        System.out.println(str.replaceAll("0","6"));    }}

引用某個對象的方法

  

@FunctionalInterface //此處為函數式介面,只允許介面定義一個方法
package com.java.demo;
@FunctionalInterface //此處為函數式介面,只可以定義一個方法
interface IMessage<R>{ // P表示方法的參數類型,R表示方法的傳回值類型 public R change();}public class TestDemo{ public static void main(String args[]){ /** *String類的轉大寫方法:public String toUpperCase(); *""hello"為String類的匿名對象,傳遞該普通方法 */ IMessage<String> msg = "hello" :: toUpperCase; String str = msg.change(); System.out.println(str);//HELLO }}

引用特定類

package com.java.demo;interface IMessage<R>{ // P表示方法的參數類型,R表示方法的傳回值類型    public R change(String str1,String str2);}public class TestDemo{    public static void main(String args[]){        /**         *String類的轉大寫方法:public int compareTo(); 字串比較,相等返回  0,小於返回<0;大約返回數字>0         *srt1.compareTo(str2);         */        IMessage<Integer> msg = String :: compareTo;                System.out.println(msg.change("java", "java"));//0    }}

引用構造方法

package com.java.demo;interface IMessage<R>{ // P表示方法的參數類型,R表示方法的傳回值類型    public R change(String title);}class Book{    private String title;    public Book(String title){        this.title = title;    }    @Override    public String toString() {                return "書名:" +this.title;    }}public class TestDemo{    public static void main(String args[]){        IMessage<Book> msg =Book :: new;        //msg.change("java開發")返回的是Book類對象        System.out.println(msg.change("java開發"));    }}

內建函數式介面:

  功能型介面:public Interface Function<T,R> ,定義的方法:public R appy(T t);接受參數也返回一個處理結果

  消費型介面:public Interface Customer<T> ,定義方法 public void appy(T t);只接受參數不返回處理結果

  供給型介面:public Interface Supper<T> 定義反方法 :public T get();不接受參數只返回處理結果

  斷言型介面:public Interface Predicate<T> 定義方法 public boolean test<T t> 接受參數返回boolean值

package com.java.demo;import java.util.function.Function;public class TestDemo{    public static void main(String args[]){         Function<String,Boolean> fun ="hello" ::  equals;         System.out.println(fun.apply("hello"));    }}

 

java中的方法引用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.