Java中, 一般來說this指針指的是當前正在訪問的這段代碼的對象 ,但是如果在內部類中需要使用外部類中的對象,這時就需要使用外部類的類名進行限定。 這種方式在Android開發中也比較常見 。
@Author: twlkyaopackage twlkyao;public class A { public A() { Inner inner = new Inner(); inner.outer(); // call the inner class's outer method. this.outer(); // call A's outer method. } public void outer() { System.out.println("outer run"); } class Inner { public void outer(){ System.out.println("inner run"); A.this.outer(); // call A's outer method. System.out.println("--------"); } } public static void main(String[] args) { A a = new A(); }}
Inner是內部類,訪問類A中的outer()方法,又由于匿名內部類中有同樣的方法,所以需要使用A的this指針進行限定。
輸出結果為:
inner runouter run--------outer run
新聞熱點
疑難解答