<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ### [Instrument 作為接口](https://lingcoder.gitee.io/onjava8/#/book/10-Interfaces?id=instrument-%e4%bd%9c%e4%b8%ba%e6%8e%a5%e5%8f%a3) 回顧下樂器的例子,使用接口的話: ![類圖](https://lingcoder.gitee.io/onjava8/images/1562737974623.png) 類**Woodwind**和**Brass**說明一旦實現了某個接口,那么其實現就變成一個普通類,可以按常規方式擴展它。 接口的工作方式使得我們不需要顯式聲明其中的方法為**public**,它們自動就是**public**的。`play()`和`adjust()`使用**default**關鍵字定義實現。在 Java 8 之前,這些定義要在每個實現中重復實現,顯得多余且令人煩惱: ~~~ // interfaces/music5/Music5.java // {java interfaces.music5.Music5} package interfaces.music5; import polymorphism.music.Note; interface Instrument { // Compile-time constant: int VALUE = 5; // static & final default void play(Note n) // Automatically public System.out.println(this + ".play() " + n); } default void adjust() { System.out.println("Adjusting " + this); } } class Wind implements Instrument { @Override public String toString() { return "Wind"; } } class Percussion implements Instrument { @Override public String toString() { return "Percussion"; } } class Stringed implements Instrument { @Override public String toString() { return "Stringed"; } } class Brass extends Wind { @Override public String toString() { return "Brass"; } } class Woodwind extends Wind { @Override public String toString() { return "Woodwind"; } } public class Music5 { // Doesn't care about type, so new types // added to the system still work right: static void tune(Instrument i) { // ... i.play(Note.MIDDLE_C); } static void tuneAll(Instrument[] e) { for (Instrument i: e) { tune(i); } } public static void main(String[] args) { // Upcasting during addition to the array: Instrument[] orchestra = { new Wind(), new Percussion(), new Stringed(), new Brass(), new Woodwind() } tuneAll(orchestra); } } ~~~ 輸出: ~~~ Wind.play() MIDDLE_C Percussion.play() MIDDLE_C Stringed.play() MIDDLE_C Brass.play() MIDDLE_C Woodwind.play() MIDDLE_C ~~~ 這個版本的例子的另一個變化是:`what()`被修改為`toString()`方法,因為`toString()`實現的正是`what()`方法要實現的邏輯。因為`toString()`是根基類**Object**的方法,所以它不需要出現在接口中。 注意到,無論是將其向上轉型為稱作**Instrument**的普通類,或稱作**Instrument**的抽象類,還是叫作**Instrument**的接口,其行為都是相同的。事實上,從`tune()`方法上看不出來**Instrument**到底是一個普通類、抽象類,還是一個接口。
                  <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>

                              哎呀哎呀视频在线观看