问题描述
我最近正在浏览一些开源代码PicketLink代码。如果您查看,你会看到一些抽象类中没有什么不同的具体方法。有没有任何目的?
I was recently looking through some open source code PicketLink code. If you take a look at this class, you'll see a number of concrete methods in an abstract class that do nothing. Is there any purpose whatsoever for this?
我想了两件事:
- 如果该方法需要被子类覆盖,而不是在父抽象类中定义,那么为什么不简单地将其抽象化?
- 如果只有类实际上需要实现该方法,这不意味着需要对类层次结构进行重组,以便孩子不被迫使用不适用的方法?
- If the method needs to be overriden by subclasses and not defined in the parent abstract class, why not simply make it abstract?
- If only some of the child classes actually need to implement the method, wouldn't this indicate the need for a restructuring of the class hierarchy so that children are not forced to have methods that are not applicable?
推荐答案
虽然不是最常见的情况,有时它在。在这种情况下,有一种定义流程的方法,将某些部分的具体实现留给其子类。在某些情况下,默认的具体行为是不做任何事情,将基类中的具体方法留空,但允许通过覆盖子类来定制子类。
While not the most common case, sometimes it is handy in the context of a template method. In this case there is a method that defines the flow, leaving the concrete implementation of some parts to its subclasses. In some cases a default concrete behavior is to do nothing, leaving the concrete method in the base class empty, but allowing customization in the subclass by overriding it.
HTH
这篇关于抽象类中有空的具体方法有什么理由吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!