<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ## [this關鍵字](https://lingcoder.gitee.io/onjava8/#/book/06-Housekeeping?id=this%e5%85%b3%e9%94%ae%e5%ad%97) 對于兩個相同類型的對象**a**和**b**,你可能在想如何調用這兩個對象的`peel()`方法: ~~~ // housekeeping/BananaPeel.java class Banana { void peel(int i) { /*...*/ } } public class BananaPeel { public static void main(String[] args) { Banana a = new Banana(), b = new Banana(); a.peel(1); b.peel(2); } } ~~~ 如果只有一個方法`peel()`,那么怎么知道調用的是對象**a**的`peel()`方法還是對象**b**的`peel()`方法呢?編譯器做了一些底層工作,所以你可以像這樣編寫代碼。`peel()`方法中第一個參數隱密地傳入了一個指向操作對象的 引用。因此,上述例子中的方法調用像下面這樣: ~~~ Banana.peel(a, 1) Banana.peel(b, 2) ~~~ 這是在內部實現的,你不可以直接這么編寫代碼,編譯器不會接受,但能說明到底發生了什么。假設現在在方法內部,你想獲得對當前對象的引用。但是,對象引用是被秘密地傳達給編譯器——并不在參數列表中。方便的是,有一個關鍵字:**this**。**this**關鍵字只能在非靜態方法內部使用。當你調用一個對象的方法時,**this**生成了一個對象引用。你可以像對待其他引用一樣對待這個引用。如果你在一個類的方法里調用該類的其他方法,不要使用**this**,直接調用即可,**this**自動地應用于其他方法上了。因此你可以像這樣: ~~~ // housekeeping/Apricot.java public class Apricot { void pick() { /* ... */ } void pit() { pick(); /* ... */ } } ~~~ 在`pit()`方法中,你可以使用`this.pick()`,但是沒有必要。編譯器自動為你做了這些。**this**關鍵字只用在一些必須顯式使用當前對象引用的特殊場合。例如,用在**return**語句中返回對當前對象的引用。 ~~~ // housekeeping/Leaf.java // Simple use of the "this" keyword public class Leaf { int i = 0; Leaf increment() { i++; return this; } void print() { System.out.println("i = " + i); } public static void main(String[] args) { Leaf x = new Leaf(); x.increment().increment().increment().print(); } } ~~~ 輸出: ~~~ i = 3 ~~~ 因為`increment()`通過**this**關鍵字返回當前對象的引用,因此在相同的對象上可以輕易地執行多次操作。 **this**關鍵字在向其他方法傳遞當前對象時也很有用: ~~~ // housekeeping/PassingThis.java class Person { public void eat(Apple apple) { Apple peeled = apple.getPeeled(); System.out.println("Yummy"); } } public class Peeler { static Apple peel(Apple apple) { // ... remove peel return apple; // Peeled } } public class Apple { Apple getPeeled() { return Peeler.peel(this); } } public class PassingThis { public static void main(String[] args) { new Person().eat(new Apple()); } } ~~~ 輸出: ~~~ Yummy ~~~ **Apple**因為某些原因(比如說工具類中的方法在多個類中重復出現,你不想代碼重復),必須調用一個外部工具方法`Peeler.peel()`做一些行為。必須使用**this**才能將自身傳遞給外部方法。
                  <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>

                              哎呀哎呀视频在线观看