[英]java, anonymous Inner class issue
Let's assume I need a specific application-wide actionListener. 假设我需要一个特定的应用程序范围的actionListener。 For example:
例如:
public class TestAnonymousInnerClass {
private ActionListener closeAction = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
...
}
This code compiles and works fine, but can I say that I have used an anonymous inner class to instantiate this closeAction
field? 这段代码编译并且工作正常,但是我可以说我使用了一个匿名内部类来实例化这个
closeAction
字段吗?
UPD: I am reading the book SG Ganesh, Tushar Sharma - Oracle Certified Professional Java SE 7 Programmer Exams 1Z0-804 and 1Z0-805 (The Expert's Voice in Java) - 2013 And here is the confusing illustration: UPD:我正在阅读SG Ganesh,Tushar Sharma - 甲骨文认证专家Java SE 7程序员考试1Z0-804和1Z0-805(Java专家的声音) - 2013这里有一个令人困惑的插图:
As follows it is not possible to have anonymous and 'non-local' class.
如下所示,不可能有匿名和“非本地”类。 But my class seems to be of that kind because it is not in context of method and still anonymous.
但我的班级似乎是那种,因为它不是在方法的背景下,仍然是匿名的。
Yes, You have created a annonymous inner implementation class of ActionListener
interface. 是的,您已经创建了一个
ActionListener
接口的匿名内部实现类。 And instantiate it and assigned to closeAction
variable 并实例化它并分配给
closeAction
变量
In short we can say that an anonymous class is an inner class that does not have a name at all. 简而言之,我们可以说匿名类是一个根本没有名字的内部类。 And whose instance is being created at the time of its creation.
并且它的实例是在创建时创建的。 Check here for more details.
点击此处了解更多详情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.