内部类中高层成员的可访问性? [英] Accessibility of members of top level class in inner class?

查看:112
本文介绍了内部类中高层成员的可访问性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于成员内部类中顶级类可访问性的查询。
我刚刚读了本地或匿名内部类只能访问最终变量的原因,原因是JVM将这两个类作为完全不同的类来处理,因此,如果一个类中变量的值发生变化,它将无法然后在运行时反映到另一个类文件中。

I have a query regarding accessibility of top level class from member inner class. I have just read the reason why local or anonymous inner classes can access only final variables.The reason being JVM handles these two classes as entirely different classes and so, if value of variable in one class changes, it can't be reflected at run time in another class file.

然后,我的问题是内部成员类(非静态)如何能够访问top成员的成员级别类,因为JVM仍将这两个类视为不同的类文件?如果顶级类的成员变量的值发生更改,那么在运行时如何在内部类的类文件中反映出来?

Then, my question is that how an inner member class (non-static) can have access to members to members of top level class, as JVM is still treating these two classes as different class files? If value of a member variable of top level class changes, how will it possible to reflect in class file of inner class at runtime?

推荐答案

<它们是单独的类,但是在内部类中隐式引用了外部类的实例。它基本上是一个变量,您可以隐式地获取它,也可以通过 ContainingClassname.this 的特殊语法获得。

请注意,如果您想要这样的隐式引用,则应将嵌套类声明为 static

Note that if you don't want such an implicit reference, you should declare the nested class as static:

public class Outer
{
    private class Inner
    {
        // There's an implicit reference to an instance of Outer in here.
        // For example:
        // Outer outer = Outer.this;
    }

    private static class Nested
    {
        // There's no implicit reference to an instance of Outer here.
    }
}

这篇关于内部类中高层成员的可访问性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆