1

例如,如果 A 有内部类 B,B 有内部类 C,都有一个属性“name”,我知道 C 可以通过 B.this.name 访问 B 中的名称,但是如何从 C 中访问 A 中的名称?

public class A{
    String name="A";
    public class B{
        String name="B";
        public class C{
            String name="C";
            public C(){
                //how to print name in A?
                //System.out.println(B.A.name);
                //System.out.println(B.A.this.name);
                //System.out.println(B.this.A.name);
                //System.out.println(B.this.A.this.name);
            }
        }
        C c=new C();
    }
    B b=new B();
    public static void main(String[] args){
        new A();
    }
}

我尝试了很多语法但它们无法编译,而且在搜索java外部类时,我发现它们中的大多数只是关于外部类,而不是外部外部类。

4

2 回答 2

2

用于A.this.name访问最外层的类。或任何其他类。

于 2015-08-05T06:42:44.720 回答
1

使用System.out.println(A.this.name);

于 2015-08-05T06:46:40.187 回答