简体   繁体   English

为什么AbstractAction不实现actionPerformed()?

[英]Why doesn't AbstractAction implement actionPerformed()?

Class AbstractAction implements interface Action , but in Action , there's a method actionPerformed(ActionEvent e) which inherits from interface ActionListener AbstractAction实现接口Action ,但是在Action ,有一个方法actionPerformed(ActionEvent e)继承自接口ActionListener
I know that the class implements an interface must supply all the implementations of methods in that interface 我知道该类实现一个接口必须提供该接口中方法的所有实现
But I found that there's no implementation of actionPerformed(ActionEvent e) in AbstractAction , why ? 但是我发现AbstractAction没有actionPerformed(ActionEvent e)的实现,为什么?

AbstractAction is an abstract class so it doesn't have to implement all of the methods on the interface. AbstractAction是一个抽象类,因此不必在接口上实现所有方法。 Abstract classes can't be instantiated so they can't be used without creating a subclass of it. 抽象类无法实例化,因此必须先创建抽象类才能使用它们。 Only concrete classes (ie non-abstract) have to provide an implementation of all methods of an interface. 只有具体的类(即非抽象类)才必须提供接口所有方法的实现。 If you subclass AbstractAction your subclass will have to implement actionPerformed() or it will have to be abstract too. 如果将AbstractAction子类化,则您的子类将必须实现actionPerformed(),或者也必须是抽象的。

Now those are the rules, but it doesn't make sense for AbstractAction to implement actionPerformed() because it couldn't possibly provide a useful implementation. 现在这些都是规则了,但是AbstractAction不能实现actionPerformed(),因为它不可能提供有用的实现。 Every subclass would have to override it's definition which makes it a good candidate for being marked abstract. 每个子类都必须重写其定义,这使其成为被标记为抽象的良好候选。

I know that the class implements an interface must supply all the implementations of methods in that interface 我知道该类实现一个接口必须提供该接口中方法的所有实现

Not true in case of an abstract class. 如果是抽象类,则不正确。 Only concrete classes are supposed to provide implementations for all the abstract methods in the superclass (which would be abstract) that it extends and all the methods in the interface it implements. 只有具体的类才能为其扩展的超类(可能是抽象的)中的所有抽象方法及其实现的接口中的所有方法提供实现。

But I found that there's no implementation of actionPerformed(ActionEvent e) in AbstractAction, why ? 但是我发现AbstractAction中没有actionPerformed(ActionEvent e)的实现,为什么?

They wanted to keep it abstract in that class to make sure that any concrete that extends this class provides an implementation for that method. 他们想让它在该类中保持抽象,以确保扩展该类的任何具体对象都可以提供该方法的实现。

Moreover, what default action would it perform (perhaps empty or nothing)? 此外,它将执行什么默认操作(也许为空或什么都不做)?

An abstract class is allowed to be incomplete. abstract类允许不完整。 Abstract classes are not required to implement any or all of the methods specified by an interface or parent abstract class. 不需要抽象类来实现接口或父抽象类指定的任何或所有方法。 Additionally, an abstract class can declare new abstract methods which any subclass must implement. 此外,抽象类可以声明任何子类都必须实现的新抽象方法。

The price paid for making a class abstract is that it may not be instantiated directly. 制作类抽象所付出的代价是它可能无法直接实例化。 That is, an abstract class must be extended to be used. 也就是说,必须扩展抽象类才能使用。

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

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