java - 类的词法包含类型

标签 java class

这就是 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

最佳答案

BarFoo 都有 A 作为直接封闭的 type 声明。他们是平等的。 Foo 也包含在 method 声明中这一事实并不影响这一点。

关于java - 类的词法包含类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27856338/

相关文章:

php - 将 PHP 类作为参数传递

c++ - 如何在方法中使用 vector 和结构成员

java - 在 WebView 中显示视频问题

java - 我应该对生成的 Java 代码进行单元测试吗?

python - super(cls, instance) 和 super(cls, subclass) 有什么区别?

python - 内部类作用域的问题

c# - 实例变量在实例化后重置为默认值

java - 如何获取具体日期?

java - 如何从字符串列表数据集创建字符串数据集 Spark Java

java - 未找到 JSP 2 标记文件 “/WEB-INF/tags”