/**
* @(#)this.java
*
*一個this指向對象的理解
* @author CplusHua
* @version 1.00 2013/5/8
*/
class AAA {
public void getthis(){
System.out.println (this);
}
public static void main(String[] args){
AAA sss=new AAA();
System.out.println (sss);//輸出對象的地址
sss.getthis();//輸出的地址與sss的地址應該是一致的,這也就是this的指向
}
}
通過上面的分析可以發現,sss和this指向的地址是一樣的,也就是說sss和this都同樣指向同一個對象。定義this的目的是因為在對象中無法事先知道外部的引用(也就是不知道會定義的名字是sss還是別的什麼),所以用this直接指向當前類所產生的當前的這個對象。