简体   繁体   English

Java初始化抽象类

[英]Java initializing abstract classes

Can someone explain this line of code for me? 有人可以为我解释这行代码吗?

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) (如果有任何相关性,抽象类也恰好是通用的)

The line above is creating an anonymous subclass of SomeAbstractClass , which will not be abstract . 上面的一行是创建SomeAbstractClass的匿名子类,它不是abstract Of course, this will work only if the base class has no abstract methods to implement. 当然,只有在基类没有要实现的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变量,从而形成闭包。

You are creating an anonymous class which is a subclass of your abstract class. 您正在创建一个匿名类,它是abstract类的子类。 Like was pointed out in comments, you are looking at an anonymous extends. 就像评论中指出的那样,你正在寻找匿名扩展。

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. 您可以对接口执行相同的操作,因为它们也可以包含abstract方法。 A practical example would be adding an ActionListener to a JButton : 一个实际的例子是将一个ActionListener添加到JButton

myJButton.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e){
                    // code
                }
            });

Java gives you the ability to create anonymous subclasses inline. Java使您能够内联创建匿名子类。 You often see this in the context of anonymous inner classes with Swing event handling, but there are many other applications as well. 您经常在具有Swing事件处理的匿名内部类的上下文中看到这一点,但是还有许多其他应用程序。

In your example, you are creating a class that extends SomeAbstractClass anonymously and assigning it to a SomeAbstractClass reference. 在您的示例中,您将创建一个匿名扩展SomeAbstractClass并将其分配给SomeAbstractClass引用的类。 It would be just as if you created a separate class like this 这就好像你创建了一个像这样的单独的类

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没有抽象方法。

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

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