<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Java 函數式接口 > 原文: [https://beginnersbook.com/2017/10/java-functional-interfaces/](https://beginnersbook.com/2017/10/java-functional-interfaces/) 只有單個抽象方法的**接口稱為函數式接口。您可以使用 Java 提供的預定義函數式接口,也可以創建自己的函數式接口并使用它。您可以在這里檢查預定義的函數式接口:[預定義的函數式接口](https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html)它們都只有一個抽象方法。這就是原因,它們也被稱為單抽象方法接口(SAM 接口)。** 要在 Java 中使用 [lambda 表達式,您需要創建自己的函數式接口或使用 Java 提供的預定義函數式接口。在創建自己的函數式接口時,用`@FunctionalInterface`注解標記它,這個注解是在 Java 8 中引入的。盡管它是可選的,你應該使用它,這樣如果你用這個標記的接口就會出現編譯錯誤注解不遵循函數式接口的規則。](https://beginnersbook.com/2017/10/java-lambda-expressions-tutorial-with-examples/) ## 定義函數式接口的規則是什么? 函數式接口應該有**只有一個**抽象方法。除了一個抽象方法,它們還可以包含任意數量的默認和靜態方法。 ## 示例 1:創建自己的函數式接口 ```java @FunctionalInterface interface MyFunctionalInterface { public int addMethod(int a, int b); } public class BeginnersBookClass { public static void main(String args[]) { // lambda expression MyFunctionalInterface sum = (a, b) -> a + b; System.out.println("Result: "+sum.addMethod(12, 100)); } } ``` 輸出: ```java Result: 112 ``` ## 示例 2:使用預定義的函數式接口 ```java import java.util.function.IntBinaryOperator; public class BeginnersBookClass { public static void main(String args[]) { // lambda expression IntBinaryOperator sum = (a, b) -> a + b; System.out.println("Result: " + sum.applyAsInt(12, 100)); } } ``` 輸出: ```java Result: 112 ``` ## 函數式接口示例:使用匿名內部類 vs 使用 lambda 表達式 **我們在 java8** 之前就已經使用了函數式接口,它們是通過使用這些接口創建匿名內部類來使用的。你必須已經看過像`Runnable`,`ActionListener`,`Comparator`等函數式接口。它們都有單一的抽象方法。讓我們看一個`ActionListener`的例子,看看它如何與`Anonymous`內部類一起使用,以及如何使用 lambda 表達式實現它。 **`ActionListener`示例:在 Java 8 之前:使用匿名內部類** ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; class Example extends JFrame { JButton button; public Example() { setTitle("Button Action Example without Lambda Expression"); setSize(400,300); setVisible(true); setLayout(new FlowLayout()); setDefaultCloseOperation(EXIT_ON_CLOSE); button = new JButton("Button"); button.setBounds(100,100,90,40); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ System.out.println("You clicked the button."); } }); add(button); } public static void main(String args[]) { new Example(); } } ``` **`ActionListener`示例:Lambda 表達式** ```java import javax.swing.*; import java.awt.*; class Example extends JFrame { JButton button; public Example() { setTitle("Button Action Example using Lambda Expression"); setSize(400,300); setVisible(true); setLayout(new FlowLayout()); setDefaultCloseOperation(EXIT_ON_CLOSE); button = new JButton("Button"); button.setBounds(100,100,90,40); //Lambda expression button.addActionListener(e-> System.out.println("You clicked the button.")); add(button); } public static void main(String args[]) { new Example(); } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看