2010-03-23 66 views
1

是否可以從封閉類訪問Java中的封閉類的陰影字段?訪問來自封閉成員類(Java)的陰影字段

public class Inherit {  

    public int a = 3; 
    private int b = 5; 
    public class Inheriting { 
     public int a = 23; 
     private int d = 8; 
     public void f() { 
      System.out.println("Here I want to get a = 3"); 
      ... 
     } 
    } 
} 

回答

2
public void f() { 
    System.out.println("Here I want to get a = 3" + Inherit.this.a); 
} 
1

是,

Inherit.this.a; 

但是你最好選擇更具描述性的名稱,以便它們不衝突。