<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ### [在構造器中調用構造器](https://lingcoder.gitee.io/onjava8/#/book/06-Housekeeping?id=%e5%9c%a8%e6%9e%84%e9%80%a0%e5%99%a8%e4%b8%ad%e8%b0%83%e7%94%a8%e6%9e%84%e9%80%a0%e5%99%a8) 當你在一個類中寫了多個構造器,有時你想在一個構造器中調用另一個構造器來避免代碼重復。你通過**this**關鍵字實現這樣的調用。 通常當你說**this**,意味著"這個對象"或"當前對象",它本身生成對當前對象的引用。在一個構造器中,當你給**this**一個參數列表時,它是另一層意思。它通過最直接的方式顯式地調用匹配參數列表的構造器: ~~~ // housekeeping/Flower.java // Calling constructors with "this" public class Flower { int petalCount = 0; String s = "initial value"; Flower(int petals) { petalCount = petals; System.out.println("Constructor w/ int arg only, petalCount = " + petalCount); } Flower(String ss) { System.out.println("Constructor w/ string arg only, s = " + ss); s = ss; } Flower(String s, int petals) { this(petals); //- this(s); // Can't call two! this.s = s; // Another use of "this" System.out.println("String & int args"); } Flower() { this("hi", 47); System.out.println("no-arg constructor"); } void printPetalCount() { //- this(11); // Not inside constructor! System.out.println("petalCount = " + petalCount + " s = " + s); } public static void main(String[] args) { Flower x = new Flower(); x.printPetalCount(); } } ~~~ 輸出: ~~~ Constructor w/ int arg only, petalCount = 47 String & int args no-arg constructor petalCount = 47 s = hi ~~~ 從構造器`Flower(String s, int petals)`可以看出,其中只能通過**this**調用一次構造器。另外,必須首先調用構造器,否則編譯器會報錯。這個例子同樣展示了**this**的另一個用法。參數列表中的變量名**s**和成員變量名**s**相同,會引起混淆。你可以通過`this.s`表明你指的是成員變量**s**,從而避免重復。你經常會在 Java 代碼中看到這種用法,同時本書中也會多次出現這種寫法。在`printPetalCount()`方法中,編譯器不允許你在一個構造器之外的方法里調用構造器。
                  <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>

                              哎呀哎呀视频在线观看