<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Java 8 中的方法引用 > 原文: [https://beginnersbook.com/2017/10/method-references-in-java-8/](https://beginnersbook.com/2017/10/method-references-in-java-8/) 在上一個教程中,我們在 Java 8 中學習了 [lambda 表達式](https://beginnersbook.com/2017/10/java-lambda-expressions-tutorial-with-examples/)。這里我們將討論 java 8 的另一個新特性,**方法引用**。方法引用是用于調用方法的 lambda 表達式的簡寫表示法。例如: 如果你的 lambda 表達式是這樣的: ```java str -> System.out.println(str) ``` 然后你可以用這樣的方法引用替換它: ```java System.out::println ``` `::`運算符用于方法引用,以將類或對象與方法名稱分開(我們將在示例的幫助下學習)。 ## 四種方法引用 1. 對象的實例方法的方法引用 - `object::instanceMethod` 2. 類的靜態方法的方法引用 - `class::staticMethod` 3. 特定類的任意對象的實例方法的方法引用 - `Class::instanceMethod` 4. 構造函數的方法引用 - `Class::new` ## 1.方法引用:對象的實例方法 ```java @FunctionalInterface interface MyInterface{ void display(); } public class Example { public void myMethod(){ System.out.println("Instance Method"); } public static void main(String[] args) { Example obj = new Example(); // Method reference using the object of the class MyInterface ref = obj::myMethod; // Calling the method of functional interface ref.display(); } } ``` 輸出: ```java Instance Method ``` ## 2.方法引用:類的靜態方法 ```java import java.util.function.BiFunction; class Multiplication{ public static int multiply(int a, int b){ return a*b; } } public class Example { public static void main(String[] args) { BiFunction<Integer, Integer, Integer> product = Multiplication::multiply; int pr = product.apply(11, 5); System.out.println("Product of given number is: "+pr); } } ``` 輸出: ```java Product of given number is: 55 ``` ## 3.方法引用:特定類型的任意對象的實例方法 ```java import java.util.Arrays; public class Example { public static void main(String[] args) { String[] stringArray = { "Steve", "Rick", "Aditya", "Negan", "Lucy", "Sansa", "Jon"}; /* Method reference to an instance method of an arbitrary * object of a particular type */ Arrays.sort(stringArray, String::compareToIgnoreCase); for(String str: stringArray){ System.out.println(str); } } } ``` 輸出: ```java Aditya Jon Lucy Negan Rick Sansa Steve ``` ## 4.構造函數的方法引用 ```java @FunctionalInterface interface MyInterface{ Hello display(String say); } class Hello{ public Hello(String say){ System.out.print(say); } } public class Example { public static void main(String[] args) { //Method reference to a constructor MyInterface ref = Hello::new; ref.display("Hello World!"); } } ``` 輸出: ```java Hello World! ```
                  <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>

                              哎呀哎呀视频在线观看