java - 嵌套类和成员类是一回事吗?

标签 java class jls

Java 术语嵌套类成员类可以互换使用吗?

来自 JLS:

A nested class is any class whose declaration occurs within the body of another class or interface.

[…]

A member class is a class whose declaration is directly enclosed in the body of another class or interface declaration

我认为成员类一词可能不包括匿名类本地类,但这只是我的猜测。

最佳答案

这是 Chapter 8. Classes 的一段很好的引用。 :

Member class declarations describe nested classes that are members of the surrounding class. Member classes may be static, in which case they have no access to the instance variables of the surrounding class; or they may be inner classes.

作为奖励报价,local classes绝对不是成员类:

A local class is a nested class that is not a member of any class [...].

<小时/>
class Foo {
    // a member class
    class InstanceMember {}
    // a member class
    static class StaticMember {}

    Foo() {
        // not a member class
        class LocalAndNotMember {}
        // not a member class
        Object anonymousAndNotMember = new Object() {};
    }
}

关于java - 嵌套类和成员类是一回事吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30111984/

相关文章:

java - 代理的原始类名(没有手动字符串操作)

java - JVM如何初始化父类(super class)和子类中的字段?

java - Java 语言规范的哪一部分描述了省略可变参数的行为?

java - 如何计算大小为 625 的数组的 O(log n) 处理时间?

java - 如何使用流 API 获取集合

java - 如何在netbeans中创建这样的Java gui应用程序

c++ - 尝试通过 .h 和 .cpp 文件设置类——为什么会出现这些错误?

jquery - 使用 jquery 获取具有分配的多个类的元素的类值

java - 为什么 Java 在数字提升期间将字节和短操作数转换为整数

java - 为什么要返回 Iterator 低耦合 (OOP)?