當一個對象創建后,Java虛擬機(JVM)就會給這個對象分配一個引用自身的指針,這個指針的名字就是this。因此,this只能在類中的非靜態方法中使用,靜態方法和靜態的代碼塊中絕對不能出現this,並且this只和特定的對象關聯,而不和類關聯,同一個類的不同對象有不同的this。
下面我們將通過一個小例子來了解一下this的相關用法與注意事項:
package test; public class ThisTest { private int i=0; //第一個構造器:有一個int型形參
ThisTest(int i){ this.i=i+1;//此時this表示引用成員變量i,而非函數參數i
System.out.println("Int constructor i——this.i: "+i+"——"+this.i); System.out.println("i-1:"+(i-1)+"this.i+1:"+(this.i+1)); //從兩個輸出結果充分證明了i和this.i是不一樣的!
} // 第二個構造器:有一個String型形參
ThisTest(String s){ System.out.println("String constructor: "+s); } // 第三個構造器:有一個int型形參和一個String型形參
ThisTest(int i,String s){ this(s);//this調用第二個構造器 //this(i); this調用第一個構造器
this.i=i++;//this以引用該類的成員變量
System.out.println("Int constructor: "+i+"/n"+"String constructor: "+s); } public ThisTest increment(){ this.i++; return this;//返回的是當前的對象,該對象屬於(ThisTest)
} public static void main(String[] args){ ThisTest tt0=new ThisTest(10); ThisTest tt1=new ThisTest("ok"); ThisTest tt2=new ThisTest(20,"ok again!"); System.out.println(tt0.increment().increment().increment().i); //tt0.increment()返回一個在tt0基礎上i++的ThisTest對象, //接着又返回在上面返回的對象基礎上i++的ThisTest對象!
} }
運行結果:
Int constructor i——this.i: 10——11
String constructor: ok
String constructor: ok again!
Int constructor: 21
String constructor: ok again!
14
總結一下,this主要注意事項:
- 表示用類的成員變量,而非函數參數,注意在函數參數和成員變量同名是進行區分。
- 用於在構造方法中引用滿足指定參數類型的構造器時必須非常注意:一個構造器中只能有一個this且必須在首位。
- this不能用在static方法中!(具體原因參見文首)
this的主要用途:
- 通過this調用另一個構造方法,用法是this(參數列表),這個僅僅能在類的構造方法中使用,其他地方不行。
- 函數參數或者函數中的局部變量和成員變量同名的情況下,成員變量被屏蔽,此時要訪問成員變量則需要用“this.成員變量名”的方式來引用成員變量。當然,在沒有同名的情況下,可以直接用成員變量的名字,但使用了this也是正確的。
- 當需要訪問另一個類的成員變量或者方法時(非靜態),除了通過內部類訪問包裝類以外。還能通過將要被訪問的類的引用(this)傳遞出去,然后在要進行訪問操作的類中將其接受並作為成員變量儲存起來。隨后便可通過該成員變量對另一個類進行訪問(該成員變量實際上就是一個指向被訪問類的指針)。 (可參考TextFieldPlus.java該程序)
綜上:這些總結都是由“this是指向對象本身的一個指針”這句話的更深入的理解而來的。
附錄:
/* TextFieldPlus author: cherryljr */ import java.awt.*; import java.awt.event.*; public class TextFieldPlus { public static void main(String[] args) { new MyFrame(); } } class MyFrame extends Frame { TextField num1, num2, num3; MyFrame() { super("PlusTest"); num1 = new TextField(10); num2 = new TextField(10); num3 = new TextField(10); Label plus = new Label("+"); Button equl = new Button("="); equl.addActionListener(new Monitor(this)); // 將當前對象的引用傳遞給Monitor setLayout(new FlowLayout()); add(num1); add(plus); add(num2); add(equl); add(num3); pack(); setVisible(true); } } class Monitor implements ActionListener { MyFrame f = null; // 還可以通過內部類實現,即無需進行傳遞直接調用包裝類(MyFrame)的成員 Monitor(MyFrame f) { this.f = f; // 重寫Monitor的構造方法,接受並儲存傳遞過來的指針以便使用 } public void actionPerformed(ActionEvent e) { int n1 = Integer.parseInt(f.num1.getText()); int n2 = Integer.parseInt(f.num2.getText()); f.num3.setText("" + (n1 + n2)); } }
《Thinking in java》上兩個關於this使用的小程序:
public class ThisDemo { String name = "cherry"; public void print(String name){ System.out.println("類中的屬性 name =" + this.name); System.out.println("局部傳參的屬性 =" + name); } public static void main(String[] args) { ThisDemo tt = new ThisDemo(); tt.print("sakura"); } }
public class ThisDemo { int number; ThisDemo increment(){ number++; return this; } private void print(){ System.out.println("number=" + number); } public static void main(String[] args) { ThisDemo tt=new ThisDemo(); tt.increment().increment().increment().print(); } }