从封闭的成员类(Java)访问阴影字段

时间:2010-03-23 13:45:32

标签: java field inner-classes

是否可以从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 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

是的,

Inherit.this.a;

但是你最好选择更具描述性的名字,这样它们就不会发生冲突。