<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 8 接口更改 - 默認方法和靜態方法 > 原文: [https://beginnersbook.com/2017/10/java-8-interface-changes-default-method-and-static-method/](https://beginnersbook.com/2017/10/java-8-interface-changes-default-method-and-static-method/) 在 java 8 之前,java 中的[接口只能有抽象方法。接口的所有方法都是公共的和默認是抽象的。 Java 8 允許接口具有默認和靜態方法。我們在接口中使用默認方法的原因是允許開發人員向接口添加新方法,而不會影響實現這些接口的類。](https://beginnersbook.com/2013/05/java-interface/) ## 為什么默認方法? 例如,如果`A`,`B`,`C`和`D`等幾個類實現了接口`XYZInterface`,那么如果我們向`XYZInterface`添加新方法,我們必須更改所有代碼實現此接口的類(`A`,`B`,`C`和`D`)。在這個例子中,我們只有四個實現我們想要改變的接口的類,但想象如果有數百個類實現一個接口,那么幾乎不可能改變所有這些類中的代碼。這就是為什么在 java 8 中,我們有一個新概念“默認方法”。這些方法可以添加到任何現有接口,我們不需要在實現類中強制實現這些方法,因此我們可以將這些默認方法添加到現有接口而不會破壞代碼。 > 我們可以說在 java 8 中引入了默認方法的概念,以這種方式在現有接口中添加新方法,使它們向后兼容。向后兼容性是在不破壞舊代碼的情況下添加新功能。 **接口中的靜態方法**類似于默認方法,除了我們不能在實現這些接口的類中覆蓋這些方法。 ## Java 8 示例:接口中的默認方法 `MyInterface`中的方法`newMethod()`是一個默認方法,這意味著我們不需要在實現類`Example`中實現這個方法。這樣我們就可以將默認方法添加到現有接口,而無需擔心實現這些接口的類。 ```java interface MyInterface{ /* This is a default method so we need not * to implement this method in the implementation * classes */ default void newMethod(){ System.out.println("Newly added default method"); } /* Already existing public and abstract method * We must need to implement this method in * implementation classes. */ void existingMethod(String str); } public class Example implements MyInterface{ // implementing abstract method public void existingMethod(String str){ System.out.println("String is: "+str); } public static void main(String[] args) { Example obj = new Example(); //calling the default method of interface obj.newMethod(); //calling the abstract method of interface obj.existingMethod("Java 8 is easy to learn"); } } ``` 輸出: ```java Newly added default method String is: Java 8 is easy to learn ``` ## Java 8 示例:接口中的靜態方法 如上所述,接口中的靜態方法與默認方法類似,因此我們不需要在實現類中實現它們。我們可以安全地將它們添加到現有接口,而無需更改實現類中的代碼。由于這些方法是靜態的,我們不能在實現類中覆蓋它們。 ```java interface MyInterface{ /* This is a default method so we need not * to implement this method in the implementation * classes */ default void newMethod(){ System.out.println("Newly added default method"); } /* This is a static method. Static method in interface is * similar to default method except that we cannot override * them in the implementation classes. * Similar to default methods, we need to implement these methods * in implementation classes so we can safely add them to the * existing interfaces. */ static void anotherNewMethod(){ System.out.println("Newly added static method"); } /* Already existing public and abstract method * We must need to implement this method in * implementation classes. */ void existingMethod(String str); } public class Example implements MyInterface{ // implementing abstract method public void existingMethod(String str){ System.out.println("String is: "+str); } public static void main(String[] args) { Example obj = new Example(); //calling the default method of interface obj.newMethod(); //calling the static method of interface MyInterface.anotherNewMethod(); //calling the abstract method of interface obj.existingMethod("Java 8 is easy to learn"); } } ``` 輸出: ```java Newly added default method Newly added static method String is: Java 8 is easy to learn ``` ## Java 8 - 抽象類與接口 通過在接口中引入默認方法,似乎[抽象類](https://beginnersbook.com/2013/05/java-abstract-class-method/)與 java 8 中的接口相同。然而,這并非完全正確,即使我們現在可以使用具體方法(帶有 body 的方法)接口就像抽象類一樣,這并不意味著它們是相同的。它們之間仍然存在很少的差異,其中之一是抽象類可以有構造函數,而在接口中我們不能有構造函數。 接口的目的是提供完全抽象,而抽象類的目的是提供部分抽象。這仍然適用。接口就像是您類的藍圖,通過引入默認方法,您可以簡單地說我們可以在接口中添加其他功能而不會影響最終用戶類。 ## 默認方法和多重繼承 當我們有兩個具有相同簽名的默認方法的接口時,可能會發生[多重繼承](https://beginnersbook.com/2013/05/java-multiple-inheritance/)問題。讓我們舉個例子。 ```java interface MyInterface{ default void newMethod(){ System.out.println("Newly added default method"); } void existingMethod(String str); } interface MyInterface2{ default void newMethod(){ System.out.println("Newly added default method"); } void disp(String str); } public class Example implements MyInterface, MyInterface2{ // implementing abstract methods public void existingMethod(String str){ System.out.println("String is: "+str); } public void disp(String str){ System.out.println("String is: "+str); } public static void main(String[] args) { Example obj = new Example(); //calling the default method of interface obj.newMethod(); } } ``` 輸出: ```java Error: Duplicate default methods named newMethod with the parameters () and () are inherited from the types MyInterface2 and MyInterface ``` 這是因為我們在接口和編譯器中都有相同的方法,不知道要調用哪個方法。 **如何解決這個問題?**為了解決這個問題,我們可以在實現類中實現這個方法,如下所示: ```java interface MyInterface{ default void newMethod(){ System.out.println("Newly added default method"); } void existingMethod(String str); } interface MyInterface2{ default void newMethod(){ System.out.println("Newly added default method"); } void disp(String str); } public class Example implements MyInterface, MyInterface2{ // implementing abstract methods public void existingMethod(String str){ System.out.println("String is: "+str); } public void disp(String str){ System.out.println("String is: "+str); } //Implementation of duplicate default method public void newMethod(){ System.out.println("Implementation of default method"); } public static void main(String[] args) { Example obj = new Example(); //calling the default method of interface obj.newMethod(); } } ``` 輸出: ```java Implementation of default method ```
                  <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>

                              哎呀哎呀视频在线观看