是真的,每个内部类需要一个封闭的实例吗? [英] Is it true that every inner class requires an enclosing instance?

查看:1248
本文介绍了是真的,每个内部类需要一个封闭的实例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

术语内部类通常被认为是指需要包含实例的嵌套类。但是,JLS规定如下:


8.1.3。内部类和封闭实例



[...]



内部类包括本地14.3),匿名(§15.9.5)和非静态成员类(§8.5)。



[...]



其声明发生在静态上下文中的内部类的实例没有词法封闭的实例。


此外,


15.9.5。匿名类声明



[...]



匿名类始终是内部类(§8.1.3);它从不 static (§8.1.1,§8.5.1)。


并且众所周知,匿名类可以在静态上下文中声明:

  class A {
int t(){return 1; }
static A a = new A(){int t(){return 2; }};
}

要大胆地描述,



new A(){} 是一个没有封闭实例的嵌套类,在静态上下文中定义,但它不是一个静态嵌套类。内部类。



我们在日常使用中是否给这些术语分配了不适当的含义?



作为相关兴趣点,此历史规范文档定义术语 $ c> static 类成员和类
是包成员都称为顶级类。它们与内部
类不同,因为顶级类可以直接使用它自己的实例
变量。



$ b $

>解决方案

从规范的角度来看,这个问题中的区别是非常有意义的:




  • 内部类具有应用于其的限制,其与封装实例的问题无关(例如,其可以不具有静态成员);


  • 静态嵌套类的概念基本上只是命名空间;



$

这些类可能被正确地称为顶层
b $ b

只是这样,从嵌套类声明中删除 static 会同时做两个单独的事情:


  1. 它使得类需要一个包围的实例;

  2. 它使类 inner

我们很少将内部视为限制;我们只关注关闭实例关注,这更加明显。



我们缺少的是一个需要包含实例的类的术语 。没有JLS定义的这样的术语,所以我们(不知道,似乎)劫持了一个相关的,但事实上本质上不同的术语,意味着。


The term inner class is conventionally taken to mean "a nested class which requires an enclosing instance". However, the JLS states as follows:

8.1.3. Inner Classes and Enclosing Instances

[...]

Inner classes include local (§14.3), anonymous (§15.9.5) and non-static member classes (§8.5).

[...]

An instance of an inner class whose declaration occurs in a static context has no lexically enclosing instances.

Also,

15.9.5. Anonymous Class Declarations

[...]

An anonymous class is always an inner class (§8.1.3); it is never static (§8.1.1, §8.5.1).

And it is well-known that an anonymous class may be declared in a static context:

class A {
  int t() { return 1; }
  static A a =  new A() { int t() { return 2; } };
}

To describe it poignantly,

new A() {} is a nested class without an enclosing instance, defined in a static context, but it is not a static nested class—it is an inner class.

Are we all assigning inappropriate meanings to these terms in day-to-day usage?

As a related point of interest, this historical specification document defines the term top-level as the opposite of inner:

Classes which are static class members and classes which are package members are both called top-level classes. They differ from inner classes in that a top-level class can make direct use only of its own instance variables.

Whereas in the common usage top-level is taken to be the opposite of nested.

解决方案

The distinctions laid out in the question make perfect sense from the specification's standpoint:

  • an inner class has restrictions applied to it, which have nothing to do with the question of enclosing instances (it may not have static members, for example);

  • the concept of a static nested class is basically just about namespacing; these classes might rightfully be termed top-level, together with what we usually assume as top-level classes.

It just so happens that removing static from a nested class declaration does two separate things at once:

  1. it makes the class require an enclosing instance;
  2. it makes the class inner.

We rarely think about inner as entailing restrictions; we only focus on the enclosing instance concern, which is much more visible. However, from the specification's viewpoint, the restrictions are a vital concern.

What we are missing is a term for a class requiring an enclosing instance. There is no such term defined by the JLS, so we have (unaware, it seems) hijacked a related, but in fact essentially different, term to mean that.

这篇关于是真的,每个内部类需要一个封闭的实例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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