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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Java `this`關鍵字 > 原文: [https://www.programiz.com/java-programming/this-keyword](https://www.programiz.com/java-programming/this-keyword) #### 在本文中,我們將通過示例了解 Java 中的`this`關鍵字,以及如何以及在何處使用它們。 ## `this`關鍵字 在 Java 中,`this`關鍵字用于引用方法或構造器中的當前對象。 例如, ```java class Main { int instVar; Main(int instVar){ this.instVar = instVar; System.out.println("this reference = " + this); } public static void main(String[] args) { Main obj = new Main(8); System.out.println("object reference = " + obj); } } ``` **輸出**: ```java this reference = com.ThisAndThat.MyClass@74a14482 object reference = com.ThisAndThat.MyClass@74a14482 ``` 在上面的示例中,我們創建了一個名為`Main`的對象`obj`。 然后,我們打印類的對象的引用`obj`和`this`。 在這里,我們可以看到`obj`和`this`的引用是相同的。 這意味著這不過是對當前對象的引用。 * * * ## 使用`this`關鍵字 在許多情況下,通常都使用`this`關鍵字。 ## 使用`this`作為歧義變量名 在 Java 中,不允許在范圍(類范圍或方法范圍)內聲明兩個或多個具有相同名稱的變量。 但是,實例變量和參數可能具有相同的名稱。 例如, ```java class MyClass { // instance variable int age; // parameter MyClass(int age){ age = age; } } ``` 在上面的程序中,實例變量和參數具有相同的名稱:`age`。 在這里,由于名稱不明確,Java 編譯器感到困惑。 在這種情況下,我們使用`this`關鍵字。 例如, 首先,讓我們看一個不使用`this`關鍵字的示例: ```java class Main { int age; Main(int age){ age = age; } public static void main(String[] args) { Main obj = new Main(8); System.out.println("obj.age = " + obj.age); } } ``` **輸出**: ```java mc.age = 0 ``` 在上面的示例中,我們將`8`作為值傳遞給了構造器。 但是,我們得到`0`作為輸出。 這是因為 Java 編譯器由于實例變量和參數之間的名稱不明確而感到困惑。 現在,讓我們使用`this`關鍵字覆蓋上面的代碼。 ```java class Main { int age; Main(int age){ this.age = age; } public static void main(String[] args) { Main obj = new Main(8); System.out.println("obj.age = " + obj.age); } } ``` **輸出**: ```java obj.age = 8 ``` 現在,我們正在獲得預期的輸出。 這是因為調用構造器時,構造器內部的`this`被調用了構造器的對象`obj`代替。 因此,將`age`變量分配給值`8`。 另外,如果參數和實例變量的名稱不同,則編譯器會自動附加`this`關鍵字。 例如,代碼: ```java class Main { int age; Main(int i) { age = i; } } ``` 等效于: ```java class Main { int age; Main(int i) { this.age = i; } } ``` * * * ### `this`和獲取器和設置器 `this`關鍵字的另一種常見用法是在類的*獲取器和設置器*中。 例如: ```java class Main { String name; // setter method void setName( String name ) { this.name = name; } // getter method String getName(){ return this.name; } public static void main( String[] args ) { Main obj = new Main(); // calling the setter and the getter method obj.setName("Toshiba"); System.out.println("obj.name: "+obj.getName()); } } ``` **輸出**: ```java obj.name: Toshiba ``` 在這里,我們使用了`this`關鍵字: * 在設置器方法中賦值 * 在獲取器方法中訪問值 * * * ## 在構造器重載中使用它 在使用[構造器重載](/java-programming/constructors#overloading)的同時,我們可能不得不從另一個構造器調用一個構造器。 在這種情況下,我們不能顯式調用構造器。 相反,我們必須使用`this`關鍵字。 在這里,我們使用`this`關鍵字的另一種形式。 即,`this()`。 讓我們舉個例子 ```java class Complex { private int a, b; // constructor with 2 parameters private Complex( int i, int j ){ this.a = i; this.b = j; } // constructor with single parameter private Complex(int i){ // invokes the constructor with 2 parameters this(i, i); } // constructor with no parameter private Complex(){ // invokes the constructor with single parameter this(0); } @Override public String toString(){ return this.a + " + " + this.b + "i"; } public static void main( String[] args ) { // creating object of Complex class // calls the constructor with 2 parameters Complex c1 = new Complex(2, 3); // calls the constructor with a single parameter Complex c2 = new Complex(3); // calls the constructor with no parameters Complex c3 = new Complex(); // print objects System.out.println(c1); System.out.println(c2); System.out.println(c3); } } ``` **輸出**: ```java 2 + 3i 3 + 3i 0 + 0i ``` 在上面的示例中,我們使用了`this`關鍵字, * 從構造器`Complex(int i)`調用構造器`Complex(int i, int j)` * 從構造器`Complex()`調用構造器`Complex(int i)` 注意這一行, ```java System.out.println(c1); ``` 在這里,當我們打印對象`c1`時,該對象將轉換為字符串。 在此過程中,將調用`toString()`。 由于我們在類內部覆蓋了`toString()`方法,因此根據該方法獲得輸出。 `this()`的巨大優勢之一是減少重復代碼的數量。 但是,在使用`this()`時應始終小心。 這是因為從另一個構造器調用構造器會增加開銷,并且這是一個緩慢的過程。 使用`this()`的另一個巨大優勢是減少重復代碼的數量。 **注意**:從另一個構造器調用一個構造器稱為顯式構造器調用。 * * * ### 作為參數傳遞 我們可以使用`this`關鍵字將當前對象作為參數傳遞給方法。 例如, ```java class ThisExample { // declare variables int x; int y; ThisExample(int x, int y) { // assign values of variables inside constructor this.x = x; this.y = y; // value of x and y before calling add() System.out.println("Before passing this to addTwo() method:"); System.out.println("x = " + this.x + ", y = " + this.y); // call the add() method passing this as argument add(this); // value of x and y after calling add() System.out.println("After passing this to addTwo() method:"); System.out.println("x = " + this.x + ", y = " + this.y); } void add(ThisExample o){ o.x += 2; o.y += 2; } } class Main { public static void main( String[] args ) { ThisExample obj = new ThisExample(1, -2); } } ``` **輸出**: ```java Before passing this to addTwo() method: x = 1, y = -2 After passing this to addTwo() method: x = 3, y = 0 ``` 在上面的示例中,在構造器`ThisExample()`中,請注意以下行: ```java add(this); ``` 在這里,我們通過將其傳遞為參數來調用`add()`方法。 由于`this`關鍵字包含對該類的對象`obj`的引用,因此我們可以在`add()`方法內更改`x`和`y`的值。
                  <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>

                              哎呀哎呀视频在线观看