Java初始化抽象类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19608214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java initializing abstract classes
提问by user2651804
Can someone explain this line of code for me?
有人可以为我解释这行代码吗?
SomeAbstractClass variable = new SomeAbstractClass() { };
SomeAbstractClass variable = new SomeAbstractClass() { };
This properly instantiaties and stores the abstract instance in the variable. What is happening? An anonymous class that extends the abstract class, maybe? Any keywords I can use to look up information about this? (the abstract class also happens to be generic if that has any relevance)
这正确地实例化了抽象实例并将其存储在变量中。怎么了?也许是一个扩展抽象类的匿名类?我可以使用任何关键字来查找有关此的信息?(如果有任何相关性,抽象类也恰好是通用的)
采纳答案by Stefano Sanfilippo
The line above is creating an anonymous subclass of SomeAbstractClass
, which will not be abstract
. Of course, this will work only if the base class has no abstract
methods to implement.
上面这行是创建 的匿名子类SomeAbstractClass
,而不会是abstract
。当然,这只有在基类没有abstract
要实现的方法时才有效。
Actually, I cannot visualize an useful instance (besides "documentation" features, see the comment below) of the line above, unless you are implementing and/or overriding methods between curly braces. That is a quite common technique if the base class/interface happens to have few methods to implement and the implementation is simple. You can even refer to the final
variables of the surrounding method and parameters, thus making a closure.
实际上,除非您在大括号之间实现和/或覆盖方法,否则我无法想象上一行的有用实例(除了“文档”功能,请参阅下面的评论)。如果基类/接口碰巧要实现的方法很少并且实现很简单,那么这是一种非常常见的技术。甚至可以引用final
周围方法和参数的变量,从而做出闭包。
回答by Little Child
You are creating an anonymous class which is a subclass of your abstract
class. Like was pointed out in comments, you are looking at an anonymous extends.
您正在创建一个匿名类,它是您abstract
类的子类。就像评论中指出的那样,您正在查看匿名扩展。
Something like follows would work if you had abstract
methods to implement:
如果您有abstract
方法可以实现,则如下所示:
MyAbstractClass someObjectOfThatClass = new MyAbstractClass(){
@Override
public void someAbstractMethod(){
}
}
You can do the same with interfaces as they can also contain abstract
methods. A practical example would be adding an ActionListener
to a JButton
:
您可以对接口执行相同操作,因为它们也可以包含abstract
方法。一个实际的例子是将ActionListener
a添加到 a JButton
:
myJButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
// code
}
});
回答by Vidya
Java gives you the ability to create anonymous subclasses inline. You often see this in the context of anonymous inner classes with Swing event handling, but there are many other applications as well.
Java 使您能够内联创建匿名子类。您经常在具有 Swing 事件处理功能的匿名内部类的上下文中看到这一点,但还有许多其他应用程序。
In your example, you are creating a class that extends SomeAbstractClass
anonymously and assigning it to a SomeAbstractClass
reference. It would be just as if you created a separate class like this
在您的示例中,您正在创建一个SomeAbstractClass
匿名扩展的类并将其分配给SomeAbstractClass
引用。就好像你创建了一个像这样的单独的类
public class SomeConcreteClass extends SomeAbstractClass {
}
and later did this
后来做了这个
SomeAbstractClass variable = new SomeConcreteClass();
As noted by @Stefano, your approach only works if your anonymous concrete class has no abstract methods, which would be true because SomeAbstractClass
has no abstract methods.
正如@Stefano 所指出的,只有当您的匿名具体类没有抽象方法时,您的方法才有效,这是正确的,因为SomeAbstractClass
没有抽象方法。