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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## [使用 .this 和 .new](https://lingcoder.gitee.io/onjava8/#/book/11-Inner-Classes?id=%e4%bd%bf%e7%94%a8-this-%e5%92%8c-new) 如果你需要生成對外部類對象的引用,可以使用外部類的名字后面緊跟圓點和**this**。這樣產生的引用自動地具有正確的類型,這一點在編譯期就被知曉并受到檢查,因此沒有任何運行時開銷。下面的示例展示了如何使用**.this**: ~~~ // innerclasses/DotThis.java // Accessing the outer-class object public class DotThis { void f() { System.out.println("DotThis.f()"); } public class Inner { public DotThis outer() { return DotThis.this; // A plain "this" would be Inner's "this" } } public Inner inner() { return new Inner(); } public static void main(String[] args) { DotThis dt = new DotThis(); DotThis.Inner dti = dt.inner(); dti.outer().f(); } } ~~~ 輸出為: ~~~ DotThis.f() ~~~ 有時你可能想要告知某些其他對象,去創建其某個內部類的對象。要實現此目的,你必須在**new**表達式中提供對其他外部類對象的引用,這是需要使用**.new**語法,就像下面這樣: ~~~ // innerclasses/DotNew.java // Creating an inner class directly using .new syntax public class DotNew { public class Inner {} public static void main(String[] args) { DotNew dn = new DotNew(); DotNew.Inner dni = dn.new Inner(); } } ~~~ 要想直接創建內部類的對象,你不能按照你想象的方式,去引用外部類的名字**DotNew**,而是必須使用外部類的對象來創建該內部類對象,就像在上面的程序中所看到的那樣。這也解決了內部類名字作用域的問題,因此你不必聲明(實際上你不能聲明)dn.new DotNew.Inner。 下面你可以看到將**.new**應用于 Parcel 的示例: ~~~ // innerclasses/Parcel3.java // Using .new to create instances of inner classes public class Parcel3 { class Contents { private int i = 11; public int value() { return i; } } class Destination { private String label; Destination(String whereTo) { label = whereTo; } String readLabel() { return label; } } public static void main(String[] args) { Parcel3 p = new Parcel3(); // Must use instance of outer class // to create an instance of the inner class: Parcel3.Contents c = p.new Contents(); Parcel3.Destination d = p.new Destination("Tasmania"); } } ~~~ 在擁有外部類對象之前是不可能創建內部類對象的。這是因為內部類對象會暗暗地連接到建它的外部類對象上。但是,如果你創建的是嵌套類(靜態內部類),那么它就不需要對外部類對象的引用。
                  <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>

                              哎呀哎呀视频在线观看