引用对象的实例方法
package LambdaTest.LanbdaTest02; import java.util.Locale; /** * FileName: PrinterDemo * Author: lps * Date: 2022/4/5 14:27 * Sign:刘品水 Q:1944900433 */ public class PrinterDemo { public static void main(String[] args) { //ps.printUpper("lps"); // usePrinter(s -> { // String result = s.toUpperCase(Locale.ROOT); // System.out.println(result); // System.out.println(s.toUpperCase(Locale.ROOT)); // // }); usePrinter(s -> System.out.println(s.toUpperCase(Locale.ROOT))); System.out.println("========"); PrintString ps = new PrintString(); usePrinter(ps::printUpper); //Lambda表达式被对象的实例方法替代的时候 他的形式参数全部传递给该方法 作为参数 } private static void usePrinter(Printer p) { p.printUpperCase("lpsjava"); } }
package LambdaTest.LanbdaTest02; /** * FileName: Printer * Author: lps * Date: 2022/4/5 14:26 * Sign:刘品水 Q:1944900433 */ public interface Printer { void printUpperCase(String s); }
package LambdaTest.LanbdaTest02; import java.util.Locale; /** * FileName: PrintString * Author: lps * Date: 2022/4/5 14:26 * Sign:刘品水 Q:1944900433 */ public class PrintString { public void printUpper(String s) { String s1 = s.toUpperCase(Locale.ROOT); System.out.println(s1); } }