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/