方法的引用
方法引用是用來直接訪問類或者實例的已經存在的方法或者構造方法,方法引用提供了一種引用而不執行方法的方式,如果抽象方法的實現恰好可以使用調用另外一個方法來實現,就有可能可以使用方法引用
方法的引用分類
靜態方法引用
如果函數式接口的實現恰好可以通過調用一個靜態方法來實現,那么就可以使用靜態方法引用(靜態方法的引用與實例方法的引用基本不受限制,只要滿足調用條件即可)
靜態方法引用示例代碼:
/** * @auther hhh * @date 2018/12/28 23:02 * @description 如果函數式接口的實現恰好可以通過調用一個靜態方法來實現,那么就可以使用靜態方法引用 * 語法:類名::staticMethod * 注意: * 方法的引用不需要括號,因為其僅僅是方法的引用,並沒有執行 * 使用的函數式接口輸入輸出必須與定義的函數式接口一致 */ public class StaticMethodUse{ static <T> String hello(T s) { return s.toString(); } static <T> String ret() { return "hello"; } static void getSize(String s) { System.out.println(s.length()); } static String toUpCase(String s){ return s.toUpperCase(); } public static void main(String[] args) { Supplier<String> supplier = () -> hello("hello"); //使用方法引用 Supplier T get(); // 1、只支持輸出參數,不支持輸入參數,所以一下方法報錯 //2、方法的引用為什么不需要括號?因為其僅僅是方法的引用,並沒有執行 //Supplier<String> stringSupplier = StaticMethodUse::hello("wqer"); Supplier<String> s = StaticMethodUse::ret; Supplier<String> supplier1 = Fun::put; Function<Integer, String> function = StaticMethodUse::hello; //方法的參數必須在調用的時候傳入,必須調用函數式接口 System.out.println("function" + function.apply(123)); //普通lambda使用 Consumer<String> consumer = size -> StaticMethodUse.getSize(size); //方法的引用為什么不需要括號?因為其僅僅是方法的引用,並沒有執行 Consumer<String> c1 = StaticMethodUse::getSize; c1.accept("hello use lambda"); //輸出參數大寫 Function<String ,String> function1 = (ss) -> ss.toUpperCase(); Function<String,String> function2 = StaticMethodUse::toUpCase; Function<String,String> function3 = Fun::toUpCase; System.out.println(function1.apply("aasdf")); System.out.println(function2.apply("aasdf")); System.out.println(function3.apply("aasdf")); } } class Fun { static String put() { return "hello"; } static String toUpCase(String s){ return s.toUpperCase(); } }
實例方法引用
如果函數式接口的實現恰好可以通過調用一個實例的實例方法來實現,那么就可以使用實例方法引用
示例代碼:
/** * @auther hhh * @date 2018/12/28 23:29 * @description 實例方法引用 * 如果函數式接口的實現恰好可以通過調用一個實例的實例方法來實現,那么就可以使用實例方法引用 * 語法: new instMethod()::method * 注意:后面不需要加括號也不需要加參數 */ public class InstantMethodUse extends Base { String put() { return "hello"; } public String toUpCase(String s) { return s.toUpperCase(); } //調用當前實例的方法 void test() { UnaryOperator<String> unaryOperator = this::toUpCase; System.out.println(unaryOperator.apply("this inst method")); //調用父類的實例方法 UnaryOperator<String> u1 = super::toUpCase; System.out.println(u1.apply("use super upCase method")); } public static void main(String[] args) { Supplier<String> stringSupplier = () -> new InstantMethodUse().put(); Supplier<String> stringSupplier1 = () -> { return new InstantMethodUse().put(); }; Supplier<String> stringSupplier2 = new InstantMethodUse()::put; //普通lambda表達式使用 Supplier<String> stringSupplier3 = () -> new InstantMethodUse().put(); System.out.println(stringSupplier2.get()); //兩種方式使用 UnaryOperator<String> unaryOperator = new InstantMethodUse()::toUpCase; //新建對象引用 InstantMethodUse instantMethodUse = new InstantMethodUse(); UnaryOperator<String> unaryOperator1 = instantMethodUse::toUpCase; System.out.println(unaryOperator.apply("lambda instMethod Use") + " === " + unaryOperator1.apply("lambda instMethod Use")); instantMethodUse.test(); } }