re 多个匿名内部类实例
这与我对此线程中提供的问题的回答有关: 内部类是轻量级的吗?
我记得在阅读中,如果您只能从单个匿名内部类创建一个对象,并且出于这个原因,如果您想说创建一个 ActionListener
类并想要创建多个来自这个类的对象(不使用反射),不使用匿名内部类,而是使用私有内部类或独立类,但人们告诉我我错了。有人可以帮我澄清一下吗?请检查链接,因为它包含更多详细信息,但如果有任何不清楚的地方,请询问!
This is in relation to my answer to a question provided in this thread: Are Inner Classes lightweight?
I remember from my reading that if you can only create one object from a single anonymous inner class, and for this reason, if you want to say create an ActionListener
class and want to create multiple objects from this one class (not using reflection), to not use an anonymous inner class but rather either a private inner class or a stand along class, but folks are telling me I'm wrong. Can someone please clarify this for me? Please check the link as it contains more details, but if anything is unclear, please ask away!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以创建任意数量的匿名类对象,你可以在代码中的一个地方创建它们(除非你复制代码)
这段代码将创建100万个同一个类的对象。
You can create any number of anonymous class objects, you can create them in one place in your code (unless you copy the code)
This code will create 1 million objects of the same class.
我不清楚。也许如果我们梳理规范,我们会发现内部类应该与普通类一样对待的证据。然而本质上,内部类依赖于外部实例,该类不存在于实例之外。这与“正常”类不同,“正常”类的存在基本上是永久的。当然,两个外部实例的两个内部类在某种程度上彼此相关,由相同的源代码创建,但这并不意味着它们必须相同甚至相等。
有证据表明 Java 设计者的意图是这样的,即内部类实际上存在于外部实例的范围内。例如,奇怪的语法
outerInstance.new InnerClass()
。例如,没有静态变量,没有内部类的静态初始值设定项。在类卸载的讨论中[1],我们看到这个论点并不真正适用于内部类,可以想象内部类是可以被卸载的!可以想象,VM 会为每个新的外部实例创建一个新的内部类。实际上并非如此,内部类确实被视为与普通类相同。但从概念上讲,我总是以不同的方式看待它们,将它们视为实例私有类。
[1] http://java.sun.com/ docs/books/jls/third_edition/html/execution.html#12.7
更新:根据 [2]
并且 [3],
因此,一个匿名类有一个二进制名称,因此只有一种运行时类型。该规范保证匿名类的不同实例具有相同的类。
[2] http://java.sun. com/docs/books/jls/third_edition/html/typesValues.html#4.3.4
[3] http://java.sun.com/docs/books/jls/third_edition/html/binaryComp.html#44909
It is unclear to me. Maybe if we comb through specs we'll find evidence that inner classes should be treated the same as normal classes. However in spirit, an inner class depends on the outer instance, the class doesn't exist beyond the instance. This is different from "normal" classes whose existence is basically perpetual. Two inner classes of two outer instances of course are somewhat related each other, being created by the same source code, yet that doesn't mean they must be identical or even equal.
There are evidence that Java designers intended this way, that an inner class in spirit lives within the scope of the outer instance. For example, the curious syntax
outerInstance.new InnerClass()
. For example, no static variables, no static initializers for inner classes. In the discussion of class unloading [1], we see that the argument doesn't really apply to inner classes, it's conceivable that inner classes can be unloaded! It is conceivable that a VM creates a new inner class for each new outer instance.Practically that's not the case, inner classes are indeed treated the same as normal classes. But conceptually, I'll always think of them differently, as instance-private classes.
[1] http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.7
Update: according to [2]
and [3]
So one anonymous class has one binary name, therefore only one run-time type. The spec guarantees us that different instances of an anonymous class have identitcal class.
[2] http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.3.4
[3] http://java.sun.com/docs/books/jls/third_edition/html/binaryComp.html#44909