Java范围规则和内部类 [英] Java scoping rules and inner classes

查看:162
本文介绍了Java范围规则和内部类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有疯狂的Java范围规则都让我头晕目眩, public static void 废话无济于事。到目前为止,我使用的所有编程语言都是词法作用域或者没有任何访问修饰符的一些近似,即内部东西捕获外部东西并且只要内部存在就可以访问外部东西。

All the crazy Java scoping rules are making my head spin and the public static void nonsense isn't helping matters. So far all the programming languages I have used either lexical scoping or some approximation of it without any access modifiers, i.e. inner stuff captures outer stuff and has access to the outer stuff as long as the inner stuff exists.

那么如何理解Java中内部类的作用域规则?他们是否可以访问在外部类中声明的变量,或者是否有一些奇怪的边缘情况我不得不担心因为所有公共静态私有的东西浮动?

So how do I make sense of the scoping rules for inner classes in Java? Do they get access to variables declared in the outer class or is there some weird edge cases I have to worry about because of all the public static private stuff floating around?

推荐答案

静态嵌套类 1 与外部类完全相同,只是它们可以访问外部类的所有成员,而不管访问限定符。它们与外部类的任何实例分开存在,因此需要引用实例才能访问外部类的任何实例变量或非静态方法。

Static nested classes1 are exactly like external classes except that they have access to all members of the outer class, regardless of access qualifier. They exist apart from any instance of the outer class, so need a reference to an instance in order to access any instance variables or non-static methods of the outer class.

非静态嵌套类(称为内部类)仅在外部类的实例的上下文中存在。构造时,它们会自动生成第二个字段,您可以使用语法 Outer.this 。内部类的每个实例都由外部类的单个实例包围。同样,静态嵌套类的所有访问权限都适用于内部类。但是因为它们已经有了外部类的实例,所以它们可以自动访问外部类的实例变量和方法。

Non-static nested classes (called inner classes) come into existence only in the context of an instance of the outer class. When constructed, they have a second this field automatically generated, which you can access from within the inner class using the syntax Outer.this. Each instance of the inner class is enclosed by a single instance of the outer class. Again, all the access privileges of static nested classes apply to inner classes. But since they already have an instance of the outer class available, they can automatically access instance variables and methods of the outer class.

对于一个很好的(非常详细的)讨论在内部类和访问说明符中,您可以阅读 内部类规范 。除其他外,它描述了嵌套类如何访问其外部类的 private 成员。更温和的阅读是嵌套课程教程

For a nice (and very detailed) discussion of inner classes and access specifiers, you can read through the Inner Class Specification. It describes, among other things, how a nested class gets access to private members of its outer class(es). A gentler read is the Nested Classes tutorial.

除了偏离主题:假设你有这种类结构:

An off-topic aside: Suppose you have this class structure:

public class O {
    public O() { ... }

    public class I { // an inner class
        public I() { ... }
        ...
    }
    ...
}

和你已经创建了一个 O 的实例:

and you've created an instance of O:

O outer = new O();

现在假设您要创建 OI的实例。你不能只使用 new OI(),因为新的 I 实例需要被特定实例包围 O 。为此,Java提供以下语法:

Now suppose you want to create an instance of O.I. you can't just use new O.I() because the new instance of I needs to be enclosed by a specific instance of O. For this, Java provides the following syntax:

O.I inner = outer.new O.I();

然后内部将有第二个字段设置为引用外部

Then inner will then have its second this field set to refer to outer.

请注意这个合格的 new 运算符语法仅用于内部类;如果 I 是一个 static 嵌套类,那就没必要了(实际上是一个错误)。

Note that this "qualified new operator" syntax is only used for inner classes; it would be unnecessary (in fact, an error) if I were a static nested class.

1   您经常会遇到静态内部类这一短语(包括,令人尴尬的是,在此答案的早期版本中)。这是不正确的术语。在Java中,内部类特别是非 static 嵌套类。

1 You'll often come across the phrase "static inner class" (including, embarrassingly, in an earlier version of this answer). This is incorrect terminology. In Java, "inner classes" are specifically non-static nested classes.

这篇关于Java范围规则和内部类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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