简体   繁体   English

封闭类成员的内部类访问

[英]inner class access of enclosing class members

I will be amazed if anyone can answer this question. 如果有人能回答这个问题,我会感到惊讶。 I am a beginner struggling immensely with the syntax and logic of nested classes in Java. 我是一个初学者,非常努力地使用Java中的嵌套类的语法和逻辑。 If you run the following program, 'a' will print instead of 'b'. 如果运行以下程序,将打印“ a”而不是“ b”。 Why? 为什么?

class MainClass
{
    public static void main(String[] args)
    {
        Outer OuterRefVar_a = new Outer('a');

        Outer OuterRefVar_b = new Outer('b');

        OuterRefVar_a.InnerTypeMember = OuterRefVar_a.new Inner();

        OuterRefVar_b.InnerTypeMember = OuterRefVar_a.InnerTypeMember;

        OuterRefVar_b.InnerTypeMember.set_innerChar_to_outerChar();

        System.out.println(OuterRefVar_b.InnerTypeMember.innerChar);
    }
}
class Outer
{
    char outerChar;

    Outer(char outerChar)
    {
        this.outerChar = outerChar;
    }

    class Inner
    {
        char innerChar;

        void set_innerChar_to_outerChar()
        {
            innerChar = outerChar;
        }
    }

    Inner InnerTypeMember;
}

That happens because while you have set the InnerTypeMember reference of object of A onto B.. 发生这种情况是因为在将A对象的InnerTypeMember引用设置为B时。

OuterRefVar_b.InnerTypeMember = OuterRefVar_a.InnerTypeMember;

The inner object of A still has a reference to it's original Outer object A and will reference its member variables. A的内部对象仍对其原始外部对象A进行引用,并将引用其成员变量。 Java implements inner classes by giving the object a secret reference to "Outer.this" which doesn't change simply by setting the InnerTypeMember on the other instance. Java通过为对象提供对“ Outer.this”的秘密引用来实现内部类,而仅通过在另一个实例上设置InnerTypeMember不会简单地更改该引用。

For example, if you had a InnerTypeMember variable within a completely different class, calling set_innerChar_to_outerChar() would still be expected to find Outer.outerChar on the object for which the inner class was original constructed. 例如,如果您在完全不同的类中具有InnerTypeMember变量,则仍应期望调用set_innerChar_to_outerChar()在为其最初构造内部类的对象上找到Outer.outerChar。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 内部类成员可从封闭类访问 - Accessibility of inner class members from enclosing class 在更高版本的 Java 中,内部类如何访问封闭类的私有成员? - How do inner class access enclosing class' private members in higher versions of Java? 在Java嵌套类中,封闭类可以访问内部类的私有成员吗? - In Java nested classes, can the enclosing class access private members of inner classes? java如何从内部内部类实现对封闭类的访问? - How java implement the access to the enclosing class from an inner inner class? 公共嵌套类的子类无法访问封闭类的私有成员 - Subclass of a public nested class is unable to access private members of enclosing class 静态嵌套类可以访问封闭类的哪些成员? - What members of the enclosing class can a static nested class access? 外部类可以访问内部类的成员吗? - Can an outer class access the members of inner class? 如何从Java中的封闭类访问内部类? - How to access an inner class from an enclosing class in Java? 无法从内部类访问封闭类的JPasswordField对象 - Can't access JPasswordField object of enclosing class from inner class 为什么我可以访问封闭类引用的私有成员 - Why can I access the private members of an enclosing class reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM