如何从内部类访问外部类字段?

How can I access OuterClass field from InnerClass?

我已经学习 Java 和 OOP 几个星期了。

昨天学习了内class和外class。我想知道当它们具有完全相同的名称时,如何从 InnerClass 访问 outerClass 的字段。

如下所示,当innerClass的名称不同时,可以从innerClass访问outerClass的字段。

public class OuterClass { 
    public String OuterName;
    public OuterClass(String OuterName) {
        this.OuterName = OuterName;
    }
    public class InstanceInnerClass {
        public String InnerName;
        public InstanceInnerClass(String InnerName) {
            this.InnerName = InnerName;
        }
        // In this method, accessing field in outer and inner class is possible. 
        // when they have different name.
        public void printAllName() {
            System.out.printf("OuterName: %s, InnerName: %s\n", OuterName, InnerName);
        }
    }
}

当他们有相同的名字时,我不知道如何访问这两个名字。

public class OuterClass { 
    public String name; // field which has same name
    public OuterClass(String name) {
        this.name = name;
    }
    public class InstanceInnerClass {
        public String name; // field which has same name
        public InstanceInnerClass(String name) {
            this.name = name;
        }
        public void printAllName() {
            // What should be written in this code 
            // to print both name fields from OuterClass and InnerClass
            System.out.printf("OuterName: %s, InnerName: %s\n", name, name);
        }
    }
}

只需执行 OuterClass.this 和您要访问的 OuterClass 中的字段名称,例如 OuterClass.this.OuterName。在您的代码中将如下所示:

public class OuterClass {
    public String name; // field which has same name
    public OuterClass(String name) {
        this.name = name;
    }
    public class InstanceInnerClass {
        public String name; // field which has same name
        public InstanceInnerClass(String name) {
            this.name = name;
        }
        public void printAllName() {
            // What should be written in this code
            // to print both name fields from OuterClass and InnerClass
            System.out.printf("OuterName: %s, InnerName: %s\n", name, OuterClass.this.name);
        }
    }
}

当您有两个同名字段,一个位于内部 class,另一个位于外部 class( 阴影),并且您尝试从内部 class 访问该字段,编译器假定您要从内部 class 访问该字段,并忽略外部 class 的另一个字段。否则,将无法选择使用哪个变量。

Shadowing

If a declaration of a type (such as a member variable or a parameter name) in a particular scope (such as an inner class or a method definition) has the same name as another declaration in the enclosing scope, then the declaration shadows the declaration of the enclosing scope. You cannot refer to a shadowed declaration by its name alone.

OuterClass 的字段可通过 OuterClass.thisInnerClass 中访问:

public void printAllName() {
    // to print both name fields from OuterClass and InnerClass
    System.out.printf("OuterName: %s, InnerName: %s\n", OuterClass.this.name, this.name);
}

测试:

OuterClass outer = new OuterClass("myOuter");
OuterClass.InstanceInnerClass inner = outer.new InstanceInnerClass("myInner");

inner.printAllName();

打印:

OuterName: myOuter, InnerName: myInner