<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://javabeginnerstutorial.com/core-java-tutorial/this-keyword-java/](https://javabeginnerstutorial.com/core-java-tutorial/this-keyword-java/) ## `this`是什么 `this`是 Java 中的關鍵字。 可以在類的*方法*或*構造器*內部使用。 它(`this`) 用作對當前對象的引用,當前對象的方法或構造器正在被調用。 `this`關鍵字可用于從實例方法或構造器中引用當前對象的任何成員。 ## `this`關鍵字和字段(實例變量) `this`關鍵字在處理變量隱藏時可能非常有用。 我們不能創建兩個具有相同名稱的實例/局部變量。 但是,創建一個實例變量&,一個具有相同名稱的局部變量或方法參數是合法的。 在這種情況下,局部變量將隱藏實例變量,這稱為變量隱藏。 ### 變量隱藏示例 ```java class JBT { int variable = 5; public static void main(String args[]) { JBT obj = new JBT(); obj.method(20); obj.method(); } void method(int variable) { variable = 10; System.out.println("Value of variable :" + variable); } void method() { int variable = 40; System.out.println("Value of variable :" + variable); } } ``` 上面程序的輸出 ```java Value of variable :10 Value of variable :40 ``` 如您在上面的示例中看到的那樣,實例變量正在隱藏,并且局部變量(或“方法參數”)的值不顯示為實例變量。 要解決此問題,請使用`this`關鍵字,并使用一個字段指向實例變量而不是局部變量。 ### `this`關鍵字用于變量隱藏的示例 ```java class JBT { int variable = 5; public static void main(String args[]) { JBT obj = new JBT(); obj.method(20); obj.method(); } void method(int variable) { variable = 10; System.out.println("Value of Instance variable :" + this.variable); System.out.println("Value of Local variable :" + variable); } void method() { int variable = 40; System.out.println("Value of Instance variable :" + this.variable); System.out.println("Value of Local variable :" + variable); } } ``` 上面程序的輸出 ```java Value of Instance variable :5 Value of Local variable :10 Value of Instance variable :5 Value of Local variable :40 ``` ## `this`關鍵字和構造器 `this`關鍵字可以在構造器內使用,以調用同一類中的另一個重載構造器。 這稱為顯式構造器調用。 如果一個類有兩個重載的構造器,一個不帶參數,另一個不帶參數,則會發生這種情況。 然后`this`關鍵字可用于從構造器中調用帶有參數的構造器,而無需使用參數。 這是必需的,因為無法顯式調用構造器。 ### `this`與構造器的示例 ```java class JBT { JBT() { this("JBT"); System.out.println("Inside Constructor without parameter"); } JBT(String str) { System.out .println("Inside Constructor with String parameter as " + str); } public static void main(String[] args) { JBT obj = new JBT(); } } ``` The output of the above program ```java Inside Constructor with String parameter as JBT Inside Constructor without parameter ``` 如您所見,`this`可用于調用同一類中的重載構造器。 **注意**: * `this`關鍵字只能是構造器中的第一個語句。 * 構造器可以具有`this`或`super`關鍵字,但不能同時具有這兩個關鍵字。 ## `this`關鍵字和方法 `this`關鍵字也可以在 Methods 內部使用,以從同一類調用另一個方法。 ### `this`關鍵字與方法的示例 ```java class JBT { public static void main(String[] args) { JBT obj = new JBT(); obj.methodTwo(); } void methodOne(){ System.out.println("Inside Method ONE"); } void methodTwo(){ System.out.println("Inside Method TWO"); this.methodOne();// same as calling methodOne() } } ``` 上面程序的輸出 ```java Inside Method TWO Inside Method ONE ``` ## `this`關鍵字作為方法參數的示例 ```java public class JBTThisAsParameter { public static void main(String[] args) { JBT1 obj = new JBT1(); obj.i = 10; obj.method(); } } class JBT1 extends JBTThisAsParameter { int i; void method() { method1(this); } void method1(JBT1 t) { System.out.println(t.i); } } ``` 如果您正確理解了`this`關鍵字,那么下一步應該是從 [Java 教程](https://javabeginnerstutorial.com/core-java-tutorial/)了解 Java 中的`static`關鍵字。 #### 參考文獻 1- [官方文檔](https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html)
                  <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>

                              哎呀哎呀视频在线观看