具有所有具体方法的抽象类
当类中的所有方法都是具体的时,是否存在一些实际的编程情况,有人可以将类声明为抽象?
Are there some practical programming situations for someone to declare a class abstract when all the methods in it are concrete?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
好吧,您可以使用模板方法模式,其中有多个覆盖点,所有覆盖点都有默认实现,但组合的默认实现本身不合法 - 任何功能实现都必须子类化。
(是的,我不喜欢模板方法模式;))
Well you could be using a template method pattern where there are multiple override points that all have default implementations but where the combined default implementations by themselves are not legal - any functional implementation must subclass.
(And yes, I dislike the template method pattern ;))
抽象类是声明为抽象的类 - 它可能包含也可能不包含抽象方法。 它们无法实例化,因此如果您有一个具有具体方法的抽象类,则可以对其进行子类化,然后可以实例化该子类。
An abstract class is a class that is declared abstract - it may or may not include abstract methods. They cannot be instantiated so if you have an abstract class with concrete methods then it can be subclassed and the subclass can then be instantiated.
想象一个接口,其声明的方法在实现时通常显示相同的默认行为。 当编写需要支持接口的类时,您必须一遍又一遍地定义所述默认行为。
为了促进具体类的实现,您可能需要提供一个抽象类,为每个方法提供默认行为。 为了支持具体类中的接口,您可以从抽象类派生并重写方法(如果它们偏离标准行为)。 这样您就可以避免重复实现相同(冗余)的默认行为。
Immagine an interface whose declared methods usually show the same default behavior when implemented. When writing a class that needs to support the interface you have to define said default behavior over and over.
To facilitate implementation of your concrete classes you might want to provide an abstract class providing default behavior for each method. To support the interface in a concrete class you can derive from the abstract class and override methods if they deviate from the standard behavior. That way you'll avoid the repeated implementation of the same (redundant) default behavior.
另一个可能的用例是装饰器,它将所有调用委托给包装的实例。 具体的装饰器实现只能重写那些添加了功能的方法:
尽管我并不真正认为有必要将 FooDecorator 声明为抽象(非抽象示例:HttpServletRequestWrapper)。
Another possible use case is a decorator which delegates all calls to the wrapped instance. A concrete decorator implementation can override only those methods where functionality is added:
Although I don't really see the necessarity to declare FooDecorator as abstract (non-abstract example: HttpServletRequestWrapper).
之前的答案已经解决了主要问题,但有一个小细节可能值得一提。
您可以有一个返回抽象类(隐藏)子类实例的工厂。 抽象类定义了结果对象上的契约,并提供了默认实现,但该类是抽象的这一事实既使其无法被直接实例化,也表明“真实”实现类的身份并不存在。发表。
Previous answers already hit the main issues, but there's a minor detail that might be worth mentioning.
You could have a factory that returns instances of (hidden) subclasses of the abstract class. The abstract class defines the contract on the resulting object, as well as providing default implementations, but the fact that the class is abstract both keeps it from being instantiated directly and also signals the fact that the identity of the "real" implementation class is not published.
想知道为什么没有人指出 MouseAdapter 的实际示例:
http://docs.oracle.com/javase/6/docs/api/java/awt/event/MouseAdapter.html
Wondering why no one has pointed to the Practical Example of MouseAdapter:
http://docs.oracle.com/javase/6/docs/api/java/awt/event/MouseAdapter.html
好问题:)
有一件事是肯定的......这当然是可能的。 krosenvold 的模板建议是这样做的一个很好的理由。
我只是想说,不能仅仅为了防止实例化而将类声明为抽象类。
Java 语言规范 第 8.1 节中提到了这一点.1.1
Nice question :)
One thing is for sure ... this is certainly possible. The template suggestion by krosenvold is one good reason for doing this.
I just want to say that a class must not be declared
abstract
just for preventing it's instantiation.This is referred in the Java Language Specification Section 8.1.1.1
当你有一个重要的类但系统无法创建该类的实例时,因为
When you have an important class but the system cannot create an instance fo this class, because
Servlet 示例:
所有方法都是具体的,
但基类本身是无用的:
DeleteAuthor.java
DeleteAuthorKeepBook.java
DeleteAuthorBurnBook.java
Servlet Example:
All methods are concrete,
but the base class is useless by itself:
DeleteAuthor.java
DeleteAuthorKeepBook.java
DeleteAuthorBurnBook.java