简体   繁体   English

匿名类是内部类的子集吗?

[英]Are anonymous classes a subset of inner class?

It might sound like a dumb question, but all anonymous classes must be defined and instantiated within an existing class;这听起来像是一个愚蠢的问题,但所有匿名类都必须在现有类中定义和实例化; therefore, they must be inner classes at the same time.因此,它们必须同时是内部类。

Anonymous classes are types of inner classes.匿名类是内部类的类型。 See http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html请参阅http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html

Quoting from there:从那里引用:

"You can also declare an inner class within the body of a method without naming the class. These classes are known as anonymous classes." “您还可以在不命名类的情况下在方法体内声明一个内部类。这些类称为匿名类。”

This is quite true.这是事实。 Your anonymus class could not be implemented outside of other classes, as a seperate class, because as it is anonymus you would not be even able to refer to it in any way.您的匿名类不能在其他类之外实现,作为一个单独的类,因为因为它是匿名类,您甚至无法以任何方式引用它。

Additional information: From JLS: An anonymous class is always an inner class (§8.1.3);附加信息:来自 JLS:匿名类始终是内部类(第 8.1.3 节); it is never static (§8.1.1, §8.5.1).它永远不会是静态的(第 8.1.1 节、第 8.5.1 节)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM