<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之旅 廣告
                ### [默認方法](https://lingcoder.gitee.io/onjava8/#/book/10-Interfaces?id=%e9%bb%98%e8%ae%a4%e6%96%b9%e6%b3%95) Java 8 為關鍵字**default**增加了一個新的用途(之前只用于**switch**語句和注解中)。當在接口中使用它時,任何實現接口卻沒有定義方法的時候可以使用**default**創建的方法體。默認方法比抽象類中的方法受到更多的限制,但是非常有用,我們將在“流式編程”一章中看到。現在讓我們看下如何使用: ~~~ // interfaces/AnInterface.java interface AnInterface { void firstMethod(); void secondMethod(); } ~~~ 我們可以像這樣實現接口: ~~~ // interfaces/AnImplementation.java public class AnImplementation implements AnInterface { public void firstMethod() { System.out.println("firstMethod"); } public void secondMethod() { System.out.println("secondMethod"); } public static void main(String[] args) { AnInterface i = new AnImplementation(); i.firstMethod(); i.secondMethod(); } } ~~~ 輸出: ~~~ firstMethod secondMethod ~~~ 如果我們在**AnInterface**中增加一個新方法`newMethod()`,而在**AnImplementation**中沒有實現它,編譯器就會報錯: ~~~ AnImplementation.java:3:error: AnImplementation is not abstract and does not override abstract method newMethod() in AnInterface public class AnImplementation implements AnInterface { ^ 1 error ~~~ 如果我們使用關鍵字**default**為`newMethod()`方法提供默認的實現,那么所有與接口有關的代碼能正常工作,不受影響,而且這些代碼還可以調用新的方法`newMethod()`: ~~~ // interfaces/InterfaceWithDefault.java interface InterfaceWithDefault { void firstMethod(); void secondMethod(); default void newMethod() { System.out.println("newMethod"); } } ~~~ 關鍵字**default**允許在接口中提供方法實現——在 Java 8 之前被禁止。 ~~~ // interfaces/Implementation2.java public class Implementation2 implements InterfaceWithDefault { @Override public void firstMethod() { System.out.println("firstMethod"); } @Override public void secondMethod() { System.out.println("secondMethod") } public static void main(String[] args) { InterfaceWithDefault i = new Implementation2(); i.firstMethod(); i.secondMethod(); i.newMethod(); } } ~~~ 輸出: ~~~ firstMethod secondMethod newMethod ~~~ 盡管**Implementation2**中未定義`newMethod()`,但是可以使用`newMethod()`了。 增加默認方法的極具說服力的理由是它允許在不破壞已使用接口的代碼的情況下,在接口中增加新的方法。默認方法有時也被稱為*守衛方法*或*虛擬擴展方法*。
                  <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>

                              哎呀哎呀视频在线观看