Java 8 : 函數式介面例子

來源:互聯網
上載者:User

標籤:lambda   java8   

Java 8為了支援lambda 運算式而引入了函數式介面。只有一個抽象方法的介面就能被當作函數式介面調用。

Runnable,Comparator,Coneable 都是一些函數式介面的例子。我們能Lambda運算式來實現這些函數式介面。

例如:

Thread t =new Thread(new Runnable(){   public void run(){     System.out.println("Runnable implemented by using Lambda Expression");   }});

這是未引入lambda之前建線程的方式。

Runnabl只有一個抽象方法,我們可以把它當做一個函數式介面。我們像下面這樣使用Lambda運算式:

Thread t = new Thread(()->{   System.out.println("Runnable implemented by using Lambda Expression");});

這裡我們只傳lambda運算式而不是Runnable對象。

聲明我們自己的函數式介面

我們可以在一個介面裡定義一個單獨的抽象方法來聲明我們自己的函數式介面。

public interface FunctionalInterfaceTest{void display();}//實現上面介面的測試類別public class FunctionInterfaceTestImpl {      public static void main(String[] args){     //老方式用匿名內部類     FunctionalInterfaceTest fit = new FunctionalInterfaceTest(){        public void display(){           System.out.println("Display from old way");        }};     fit.display();//outputs: Display from old way     //用lambda運算式     FunctionalInterfaceTest newWay = () -> {System.out.println("Display from new Lambda Expression");}        newWay.display();//outputs : Display from new Lambda Expression     }}

我們可以加上@FunctionalInterface 註解,來顯示編譯時間錯誤。這個可選
例如:

@FunctionalInterfacepublic interface FunctionalInterfaceTest{   void display();   void anotherDisplay();//報錯, FunctionalInterface應該只有一個抽象方法}
預設方法

函數式介面只能有一個抽象方法但可以有多個預設方法。

預設方法在Java 8中引入的,為介面添加了新方法而不會影響實作類別。

interface DefaultInterfaceTest{  void show();  default void display(){     System.out.println("Default method from interface can have body..!");  }}public class DefaultInterfaceTestImpl implements DefaultInterfaceTest{   public void show(){         System.out.println("show method");   }   //我們不需要實現預設方法   public static void main(String[] args){          DefaultInterfaceTest obj = new DefaultInterfaceTestImpl();          obj.show();//輸出: show method          obj.display();//輸出 : Default method from interface can have body..!        }}

預設方法的主要用途是沒有強制實作類別,我們能給介面添加一個方法(非抽象)。

多重實現

如果相同的預設方法出現在兩個介面裡,而一個類實現了這兩個介面,這裡就會拋出一個錯誤。

//帶show()方法的普通介面interface Test{  default void show(){     System.out.println("show from Test");  }}//有相同show()方法的另一介面interface AnotherTest{   default void show(){      System.out.println("show from Test");   }}//Main類實現兩個介面class Main implements Test, AnotherTest{//這裡的show()方法有繼承歧義}

這個類不能編譯因為Test,AnotherTest介面的show()方法有歧義,為瞭解決這個問題我們需要在Main類裡面來重寫show()方法。

class Main implements Test, AnotherTest{   void show(){      System.out.println("Main show method");   }}

Java 8 : 函數式介面例子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.