这就是 JLS 8.1.3定义词法封闭类型概念:
A class or interface O is the zeroth lexically enclosing type declaration of itself.
A class O is the n'th lexically enclosing type declaration of a class C if it is the immediately enclosing type declaration of the n-1'th lexically enclosing type declaration of C.
因此,从这个定义中并不清楚是否考虑任何内部类或只是类或接口(interface) O 的内部类。这些是不同的概念,因为最后一个定义如下:
An inner class C is a direct inner class of a class or interface O if O is the immediately enclosing type declaration of C and the declaration of C does not occur in a static context.
A class C is an inner class of class or interface O if it is either a direct inner class of O or an inner class of an inner class of O.
为了更清楚,让我举个例子:
class A{
class Bar{ } //inner class of class A
public static void main(String[] args){
class Foo{ } //inner class, but not an inner class of class A
}
}
<强> DEMO
立即封闭类型的定义被视为任何内部类或 inner classes of class or interface O
?
最佳答案
Bar
和 Foo
都有 A
作为直接封闭的 type 声明。他们是平等的。 Foo
也包含在 method 声明中这一事实并不影响这一点。
关于java - 类的词法包含类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27856338/