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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Java 9 - 接口中的私有方法 > 原文: [https://beginnersbook.com/2018/05/java-9-private-methods-in-interfaces-with-examples/](https://beginnersbook.com/2018/05/java-9-private-methods-in-interfaces-with-examples/) 我們知道 [Java 8](https://beginnersbook.com/2017/10/java-8-features-with-examples/) 允許我們在接口中創建[默認和靜態方法](https://beginnersbook.com/2017/10/java-8-interface-changes-default-method-and-static-method/)。目的是在接口上添加新方法,而不會破壞已經實現這些接口的類。[Java 9](https://beginnersbook.com/2018/04/java-9-features-with-examples/) 引入了另一個新功能,Java 9 SE 以后我們可以在接口中擁有**私有方法**。在本指南中,我們將了解為什么他們添加了此功能,它的用途以及如何使用它。 ## 為什么 Java 9 允許我們在接口中使用私有方法? Java 9 在接口中引入了**私有方法**,通過私有方法共享多個默認方法的公共代碼來刪除冗余。 為了理解這一點,我們必須在 Java 8 中使用一個示例(沒有私有方法),然后我們將使用 Java 9(使用私有方法)采用相同的示例。 **Java 8 中的示例 - 具有重復代碼的多個默認方法(公共代碼)** 在此示例中,我們將看到默認方法如何具有重復代碼,這些代碼不必要地增加代碼行并使代碼更少 - 可讀。我們將使用私有方法再次使用相同的示例來查看私有方法如何幫助我們避免重復的代碼。 ```java interface MyInterfaceInJava8 { default void method1() { System.out.println("Starting method"); System.out.println("Doing someting"); System.out.println("This is method1"); } default void method2() { System.out.println("Starting method"); System.out.println("Doing someting"); System.out.println("This is method2"); } } public class JavaExample implements MyInterfaceInJava8{ public static void main(String args[]) { JavaExample je = new JavaExample(); je.method1(); je.method2(); } } ``` 輸出: ![Java 8 - default methods with common code](https://img.kancloud.cn/ad/c9/adc91df7d455cb8b55b2d9aa37049b89_852x300.jpg) **Java 9 中的示例 - 使用私有方法共享公共代碼的默認方法** 我們采用的是上面我們看到的相同示例。這次我們將介紹一種私有方法來共享公共代碼。 ```java interface MyInterfaceInJava9 { default void method1() { //calling private method printLines(); System.out.println("This is method1"); } default void method2() { //calling private method printLines(); System.out.println("This is method2"); } private void printLines() { System.out.println("Starting method"); System.out.println("Doing someting"); } } public class JavaExample implements MyInterfaceInJava9{ public static void main(String args[]) { JavaExample je = new JavaExample(); je.method1(); je.method2(); } } ``` 輸出: ```java Starting method Doing someting This is method1 Starting method Doing someting This is method2 ``` 如您所見,輸出相同,代碼大小已減少。 基于此,我們可以說在接口中擁有私有方法的優點是: 1. 允許默認方法共享公共代碼以避免重復代碼(冗余) 2. 提高代碼可讀性。 ## Java 9 - 私有靜態方法 到目前為止,我們已經傾向于如何在接口中使用私有方法來共享默認方法的公共代碼。 Java 9 還允許我們在接口中使用**私有靜態方法**。 從 java 8 開始,我們可以在接口中使用靜態方法和默認方法。我們不能使用非靜態私有方法共享靜態方法的公共代碼,我們必須使用私有靜態方法來做到這一點。 讓我們舉一個例子來理解這一點。 ```java interface MyInterfaceInJava9 { static void method1() { //calling private method printLines(); System.out.println("This is method1"); } static void method2() { //calling private method printLines(); System.out.println("This is method2"); } //this must be static else we will get compilation error private static void printLines() { System.out.println("Starting method"); System.out.println("Doing someting"); } default void mymethods() { method1(); method2(); } } public class JavaExample implements MyInterfaceInJava9{ public static void main(String args[]) { JavaExample je = new JavaExample(); je.mymethods(); } } ``` 輸出: ```java Starting method Doing someting This is method1 Starting method Doing someting This is method2 ``` 這是 Eclipse Oxygen 的截圖。 ![Java 9 private methods in interface](https://img.kancloud.cn/5b/f6/5bf665c2699563e4f32e51ba3d647699_920x1010.jpg)
                  <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>

                              哎呀哎呀视频在线观看