Java8 Pratice - Lambda -1

先用最簡單的 Runnable 做表示,再Lambda 可以表示成以下

package com.kirin.java8;

public class TestMain {
 
 private Runnable runnable;

 public void setInterface(Runnable r){
  this.runnable = r;
 }
 
 public static void main(String[] args){
  Runnable r = () -> System.out.println("Hi~"); // A
  r = () ->{  // B
   System.out.println("v");
   System.out.println("vc");
  };
  TestMain tm = new TestMain();
  tm.setInterface(() -> System.out.println("Hi~")); //C
 }
}


A : () <= 代表是 發法中的參數,因為是 runnable 所以參數是空的
B : 如果想要寫多行程式碼
C : 可以直接在參數內實作

上述第C點,如果在TestMain 如果有第2個參數為空的interface,就會有問題, 如下


package com.kirin.java8;

public class TestMain {
 
 private Runnable runnable;
 private OtherInter otherInter;

 public void setInterface(Runnable r){
  this.runnable = r;
 }
 
 public void setInterface(OtherInter oi){
  this.otherInter = oi;
 }
 
 public static void main(String[] args){
  TestMain tm = new TestMain();
  tm.setInterface(() -> System.out.println("Hi~")); //C
 }
}

interface OtherInter{
 void testMethod();
}
這時候紅色部分就會出現 Compile 錯誤 , 但是如果是參數個數不同就不會發生此錯誤,結果如下



package com.kirin.java8;

public class TestMain {
 
 private Runnable runnable;
 private OtherInter otherInter;

 public void setInterface(Runnable r){
  this.runnable = r;
 }
 
 public void setInterface(OtherInter oi){
  this.otherInter = oi;
 }
 
 public static void main(String[] args){
  TestMain tm = new TestMain();
  tm.setInterface(() -> System.out.println("Hi~")); //C
  tm.setInterface((s) -> System.out.println("Hi~"+s)); //D
 }
}

interface OtherInter{
 void testMethod(String a); //E
}
E 增加了參數後,就不會出現Compile error  ,那延伸這個問題,如果有兩個都有一個參數的 Interface 會發生什麼問題,如下


package com.kirin.java8;

public class TestMain {
 
 private Runnable runnable;
 private OtherInter otherInter;
 private OtherInter2 otherInter2;

 public void setInterface(Runnable r){
  this.runnable = r;
 }
 
 public void setInterface(OtherInter oi){
  this.otherInter = oi;
 }
 
 public void setInterface(OtherInter2 oi2){
  this.otherInter2 = oi2;
 }
 
 
 public static void main(String[] args){
  TestMain tm = new TestMain();
  tm.setInterface(() -> System.out.println("Hi~")); //C
  tm.setInterface((s) -> System.out.println("Hi~"+s)); //D
 }
}

interface OtherInter{
 void testMethod(String a);
}
interface OtherInter2{
 void testMethod(Integer i);
}

此時D因為 compiler 因為無法確認是要傳到那一個interface ,額導致錯亂,因此Lambda 提供可以指定型別到參數中的方法 
tm.setInterface((String s) -> System.out.println("Hi~"+s));

這樣就可指定要設定哪個 interface



package com.kirin.java8;

public class TestMain {
 
 private Runnable runnable;
 private OtherInter otherInter;
 private OtherInter2 otherInter2;

 public void setInterface(Runnable r){
  this.runnable = r;
 }
 
 public void setInterface(OtherInter oi){
  this.otherInter = oi;
 }
 
 public void setInterface(OtherInter2 oi2){
  this.otherInter2 = oi2;
 }
 
 
 public static void main(String[] args){
  TestMain tm = new TestMain();
  tm.setInterface(() -> System.out.println("Hi~")); //C
  tm.setInterface((String s) -> System.out.println("Hi~"+s)); //D
  tm.setInterface((Integer s) -> System.out.println("Hi~"+s)); //E
 }
}

interface OtherInter{
 void testMethod(String a);
}
interface OtherInter2{
 void testMethod(Integer i);
}




留言

這個網誌中的熱門文章

angular 如何Http 如何設定 CORS (Cross-Origin Resource Sharing)

Google Map 單車路徑計算坡度和角度小工具

Google URL Shortener API 快速教學