<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ## [向上轉型回顧](https://lingcoder.gitee.io/onjava8/#/book/09-Polymorphism?id=%e5%90%91%e4%b8%8a%e8%bd%ac%e5%9e%8b%e5%9b%9e%e9%a1%be) 在上一章中,你看到了如何把一個對象視作它的自身類型或它的基類類型。這種把一個對象引用當作它的基類引用的做法稱為向上轉型,因為繼承圖中基類一般都位于最上方。 同樣你也在下面的音樂樂器例子中發現了問題。即然幾個例子都要演奏樂符(**Note**),首先我們先在包中單獨創建一個 Note 枚舉類: ~~~ // polymorphism/music/Note.java // Notes to play on musical instruments package polymorphism.music; public enum Note { MIDDLE_C, C_SHARP, B_FLAT; // Etc. } ~~~ 枚舉已經在”第 6 章初始化和清理“一章中介紹過了。 這里,**Wind**是一種**Instrument**;因此,**Wind**繼承**Instrument**: ~~~ // polymorphism/music/Instrument.java package polymorphism.music; class Instrument { public void play(Note n) { System.out.println("Instrument.play()"); } } // polymorphism/music/Wind.java package polymorphism.music; // Wind objects are instruments // because they have the same interface: public class Wind extends Instrument { // Redefine interface method: @Override public void play(Note n) { System.out.println("Wind.play() " + n); } } ~~~ **Music**的方法`tune()`接受一個**Instrument**引用,同時也接受任何派生自**Instrument**的類引用: ~~~ // polymorphism/music/Music.java // Inheritance & upcasting // {java polymorphism.music.Music} package polymorphism.music; public class Music { public static void tune(Instrument i) { // ... i.play(Note.MIDDLE_C); } public static void main(String[] args) { Wind flute = new Wind(); tune(flute); // Upcasting } } ~~~ 輸出: ~~~ Wind.play() MIDDLE_C ~~~ 在`main()`中你看到了`tune()`方法傳入了一個**Wind**引用,而沒有做類型轉換。這樣做是允許的——**Instrument**的接口一定存在于**Wind**中,因此**Wind**繼承了**Instrument**。從**Wind**向上轉型為**Instrument**可能“縮小”接口,但不會比**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>

                              哎呀哎呀视频在线观看