Lambda 函数式接口 Supplier

 
 
 
 
1 @FunctionalInterface 2 public interface MyFun { 3 void method(); 4 } 5 6 public class MyFunImpl implements MyFun { 7 @Override 8 public void method() { 9 System.out.println("impl method ."); 10 } 11 } 12 13 14 public class FunDemo { 15 public static void main(String[] args) { 16 show(new MyFunImpl()); 17 show(new MyFun() { 18 @Override 19 public void method() { 20 System.out.println("override interface method ."); 21 } 22 }); 23 show(() -> System.out.println("lambda method .")); 24 } 25 26 private static void show(MyFun myFun) { 27 myFun.method(); 28 } 29 }
 
 

 
1 @FunctionalInterface 2 public interface MessageBuileder { 3 String message(); 4 } 5 6 public class LogDemo { 7 public static void main(String[] args) { 8 String mes1 = "hello "; 9 String mes2 = "java "; 10 String mes3 = "world "; 11 showInfo(1, () -> mes1 + mes2 + mes3); 12 showInfo(2, () -> { 13 System.out.println("!=1"); 14 return mes1 + mes2 + mes3; 15 }); 16 } 17 18 private static void showInfo(int level, MessageBuileder m) { 19 if (level == 1) { 20 System.out.println(m.message()); 21 } 22 } 23 }
 
 
 
1 public class RunableDemo { 2 public static void main(String[] args) { 3 startThead(new Runnable() { 4 @Override 5 public void run() { 6 System.out.println(Thread.currentThread().getName() + " override run ."); 7 } 8 }); 9 startThead(() -> System.out.println(Thread.currentThread().getName() + " lambda run .")); 10 } 11 12 private static void startThead(Runnable run) { 13 new Thread(run).start(); 14 } 15 }
 
 
 
 
 
                    
                
 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号