this 關鍵字用來表示當前對象本身,或當前類的一個實例,通過 this 可以調用本對象的所有方法和屬性。例如:
運行結果:
x + y = 25
上面的程序中,obj 是 Demo 類的一個實例,this 與 obj 等價,執行 int z = this.x + this.y;,就相當於執行 int z = obj.x + obj.y;。
注意:this 只有在類實例化后才有意義。
運行結果:
網站的名字是微學苑,已經成立了3年
形參的作用域是整個方法體,是局部變量。在Demo()中,形參和成員變量重名,如果不使用this,訪問到的就是局部變量name和age,而不是成員變量。在 say() 中,我們沒有使用 this,因為成員變量的作用域是整個實例,當然也可以加上 this:
Java 默認將所有成員變量和成員方法與 this 關聯在一起,因此使用 this 在某些情況下是多余的。
運行結果:
網站的名字是微學苑,已經成立了3年
值得注意的是:
上述代碼涉及到方法重載,即Java允許出現多個同名方法,只要參數不同就可以。后續章節會講解。
運行結果:
Hello from A!
Hello from B!
匿名對象就是沒有名字的對象。如果對象只使用一次,就可以作為匿名對象,代碼中 new B(this).print(); 等價於 ( new B(this) ).print();,先通過 new B(this) 創建一個沒有名字的對象,再調用它的方法。
- public class Demo{
- public int x = 10;
- public int y = 15;
- public void sum(){
- // 通過 this 點取成員變量
- int z = this.x + this.y;
- System.out.println("x + y = " + z);
- }
- public static void main(String[] args) {
- Demo obj = new Demo();
- obj.sum();
- }
- }
x + y = 25
上面的程序中,obj 是 Demo 類的一個實例,this 與 obj 等價,執行 int z = this.x + this.y;,就相當於執行 int z = obj.x + obj.y;。
注意:this 只有在類實例化后才有意義。
使用this區分同名變量
成員變量與方法內部的變量重名時,希望在方法內部調用成員變量,怎么辦呢?這時候只能使用this,例如:- public class Demo{
- public String name;
- public int age;
- public Demo(String name, int age){
- this.name = name;
- this.age = age;
- }
- public void say(){
- System.out.println("網站的名字是" + name + ",已經成立了" + age + "年");
- }
- public static void main(String[] args) {
- Demo obj = new Demo("微學苑", 3);
- obj.say();
- }
- }
網站的名字是微學苑,已經成立了3年
形參的作用域是整個方法體,是局部變量。在Demo()中,形參和成員變量重名,如果不使用this,訪問到的就是局部變量name和age,而不是成員變量。在 say() 中,我們沒有使用 this,因為成員變量的作用域是整個實例,當然也可以加上 this:
- public void say(){
- System.out.println("網站的名字是" + this.name + ",已經成立了" + this.age + "年");
- }
作為方法名來初始化對象
也就是相當於調用本類的其它構造方法,它必須作為構造方法的第一句。示例如下:- public class Demo{
- public String name;
- public int age;
- public Demo(){
- this("微學苑", 3);
- }
- public Demo(String name, int age){
- this.name = name;
- this.age = age;
- }
- public void say(){
- System.out.println("網站的名字是" + name + ",已經成立了" + age + "年");
- }
- public static void main(String[] args) {
- Demo obj = new Demo();
- obj.say();
- }
- }
網站的名字是微學苑,已經成立了3年
值得注意的是:
- 在構造方法中調用另一個構造方法,調用動作必須置於最起始的位置。
- 不能在構造方法以外的任何方法內調用構造方法。
- 在一個構造方法內只能調用一個構造方法。
上述代碼涉及到方法重載,即Java允許出現多個同名方法,只要參數不同就可以。后續章節會講解。
作為參數傳遞
需要在某些完全分離的類中調用一個方法,並將當前對象的一個引用作為參數傳遞時。例如:- public class Demo{
- public static void main(String[] args){
- B b = new B(new A());
- }
- }
- class A{
- public A(){
- new B(this).print(); // 匿名對象
- }
- public void print(){
- System.out.println("Hello from A!");
- }
- }
- class B{
- A a;
- public B(A a){
- this.a = a;
- }
- public void print() {
- a.print();
- System.out.println("Hello from B!");
- }
- }
Hello from A!
Hello from B!
匿名對象就是沒有名字的對象。如果對象只使用一次,就可以作為匿名對象,代碼中 new B(this).print(); 等價於 ( new B(this) ).print();,先通過 new B(this) 創建一個沒有名字的對象,再調用它的方法。
<上一節下一節>
被頂起來的評論
package thisfangfa;
public class This {
public static void main(String[] args){
B b=new B(new A());//這里創建B類的對象b時,對B類的構造函數傳入了
//A類的對象的引用(指針),
}
}
class A{
public A()
{
System.out.println www.uuweb.cn ("step 1 ");
new B(this).print();//程序第一步會執行A類的構造方法A(),因為創建B類
//的對象b時,對B類的構造函數傳入了A類的對象的引用(指針),所以程序先執行
//new A() ,執行new A()的時候就運行了A類的構造方法。A類的構造方法里面
//創建了B類的匿名對象,並且引用了B類匿名對象的print();方法。
}
public void print()
{
System.out.println("step 3");//由於第二步a.print();執行了A類的
//print()方法。所以程序第三步會運行到這里。
System.out.println("hello from A");//然后執行到這句
//代碼,所以打印輸出了hello from A
}
}
class B{
A a;//這里先聲明一個A類的對象a,這時還沒有對a實例化,只是聲明。
public B(A a){//執行new A()完畢后,開始執行執行new B()了,這時會執行B類的
//構造函數B(),
this.a=a;//B b=new B(new A());//這里創建B類的對象b時,傳入的A類對象
//的引用(指針),賦予了之前聲明的A類的對象a,這時a存儲着創建B類的對象b時,傳
//入的A類對象的引用(指針)。
}
public void print()
{ System.out.println("step 2 ");//由於引用了B類匿名對象的print()方法。
//所以程序第二步會執行到這個方法里面。
a.print();//因為a存儲着創建B類的對象b時,傳入的A類對象的引用(指針)。
//所以這里執行a對象的方法print(),就是執行的A這個類里面的
//print()方法。所以程序第二步會運行到這里。這里執行了A類的print()方法。
//
//打印輸出hello from A完畢后,a.print();執行完畢,
System.out.println("step 4 ");//所以開始執行這句
System.out.println("hello from B");//和執行這句
//整個程序結束在這里。
}
}
在Eclipse下程序輸出結果是這樣的:
step 1
step 2
step 3
hello from A
step 4
hello from B