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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## [內部類與向上轉型](https://lingcoder.gitee.io/onjava8/#/book/11-Inner-Classes?id=%e5%86%85%e9%83%a8%e7%b1%bb%e4%b8%8e%e5%90%91%e4%b8%8a%e8%bd%ac%e5%9e%8b) 當將內部類向上轉型為其基類,尤其是轉型為一個接口的時候,內部類就有了用武之地。(從實現了某個接口的對象,得到對此接口的引用,與向上轉型為這個對象的基類,實質上效果是一樣的。)這是因為此內部類-某個接口的實現-能夠完全不可見,并且不可用。所得到的只是指向基類或接口的引用,所以能夠很方便地隱藏實現細節。 我們可以創建前一個示例的接口: ~~~ // innerclasses/Destination.java public interface Destination { String readLabel(); } ~~~ ~~~ // innerclasses/Contents.java public interface Contents { int value(); } ~~~ 現在**Contents**和**Destination**表示客戶端程序員可用的接口。記住,接口的所有成員自動被設置為**public**。 當取得了一個指向基類或接口的引用時,甚至可能無法找出它確切的類型,看下面的例子: ~~~ // innerclasses/TestParcel.java class Parcel4 { private class PContents implements Contents { private int i = 11; @Override public int value() { return i; } } protected final class PDestination implements Destination { private String label; private PDestination(String whereTo) { label = whereTo; } @Override public String readLabel() { return label; } } public Destination destination(String s) { return new PDestination(s); } public Contents contents() { return new PContents(); } } public class TestParcel { public static void main(String[] args) { Parcel4 p = new Parcel4(); Contents c = p.contents(); Destination d = p.destination("Tasmania"); // Illegal -- can't access private class: //- Parcel4.PContents pc = p.new PContents(); } } ~~~ 在**Parcel4**中,內部類**PContents**是**private**,所以除了**Parcel4**,沒有人能訪問它。普通(非內部)類的訪問權限不能被設為**private**或者**protected**;他們只能設置為**public**或**package**訪問權限。 **PDestination**是**protected**,所以只有**Parcel4**及其子類、還有與**Parcel4**同一個包中的類(因為**protected**也給予了包訪問權)能訪問**PDestination**,其他類都不能訪問**PDestination**,這意味著,如果客戶端程序員想了解或訪問這些成員,那是要受到限制的。實際上,甚至不能向下轉型成**private**內部類(或**protected**內部類,除非是繼承自它的子類),因為不能訪問其名字,就像在**TestParcel**類中看到的那樣。 **private**內部類給類的設計者提供了一種途徑,通過這種方式可以完全阻止任何依賴于類型的編碼,并且完全隱藏了實現的細節。此外,從客戶端程序員的角度來看,由于不能訪問任何新增加的、原本不屬于公共接口的方法,所以擴展接口是沒有價值的。這也給 Java 編譯器提供了生成高效代碼的機會。
                  <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>

                              哎呀哎呀视频在线观看