package com.fgy.demo2; public interface Printable { void print(String s); }
package com.fgy.demo2; public class Demo01Printable { public static void main(String[] args) { // printString(s -> System.out.println(s)); // // 使用方法引用进行优化 printString(System.out::println); } private static void printString(Printable p) { p.print("HelloWord"); } }
===================================================================
package com.fgy.demo3; public interface Printable { void print(String s); }
package com.fgy.demo3; public class MethodRerObject { public void printUpperCaseString(String s) { System.out.println(s.toUpperCase()); } }
package com.fgy.demo3; /** * 通过对象名引用成员方法 */ public class Demo01ObjectMethodReference { public static void main(String[] args) { /*printString(s -> { MethodRerObject obj = new MethodRerObject(); obj.printUpperCaseString(s); });*/ // 使用方法引用进行优化 printString(new MethodRerObject()::printUpperCaseString); } private static void printString(Printable p) { p.print("Hello word"); } }