java 具有所有具体方法的抽象类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/362446/
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
Abstract class with all concrete methods
提问by Saravanan M
Are there some practical programming situations for someone to declare a class abstract when all the methods in it are concrete?
当类中的所有方法都是具体的时,是否有一些实际的编程情况让某人声明类抽象?
采纳答案by krosenvold
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 ;))
(是的,我不喜欢模板方法模式;))
回答by andHapp
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.
抽象类是声明为抽象的类——它可能包含也可能不包含抽象方法。它们不能被实例化,所以如果你有一个带有具体方法的抽象类,那么它可以被子类化,然后子类可以被实例化。
回答by lithander
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.
为了促进具体类的实现,您可能希望提供一个抽象类,为每个方法提供默认行为。为了在具体类中支持接口,您可以从抽象类派生并覆盖偏离标准行为的方法。这样您就可以避免重复实现相同(冗余)的默认行为。
回答by Markus
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:
另一个可能的用例是装饰器,它将所有调用委托给包装的实例。一个具体的装饰器实现只能覆盖那些添加了功能的方法:
public interface Foo {
public void bar();
}
public abstract class FooDecorator implements Foo {
private final Foo wrapped;
public FooDecorator(Foo wrapped) { this.wrapped = wrapped; }
public void bar() { wrapped.bar(); }
}
public class TracingFoo extends FooDecorator {
//Omitting constructor code...
public void bar() {
log("Entering bar()");
super.bar();
log("Exiting bar()");
}
}
Although I don't really see the necessarity to declare FooDecorator as abstract (non-abstract example: HttpServletRequestWrapper).
虽然我真的没有看到将 FooDecorator 声明为抽象的必要性(非抽象示例:HttpServletRequestWrapper)。
回答by joel.neely
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.
您可以拥有一个返回抽象类(隐藏)子类实例的工厂。抽象类在结果对象上定义契约,并提供默认实现,但该类是抽象的这一事实既防止它被直接实例化,也表明“真实”实现类的身份不是发表。
回答by Sandeep Jindal
Wondering why no one has pointed to the Practical Example of MouseAdapter:
想知道为什么没有人指出 MouseAdapter 的实用示例:
http://docs.oracle.com/javase/6/docs/api/java/awt/event/MouseAdapter.html
http://docs.oracle.com/javase/6/docs/api/java/awt/event/MouseAdapter.html
An abstract adapter class for receiving mouse events. The methods in this class are empty. This class exists as convenience for creating listener objects.
用于接收鼠标事件的抽象适配器类。这个类中的方法是空的。此类的存在是为了方便创建侦听器对象。
回答by J.M.I. MADISON
Servlet Example:
服务程序示例:
All methods are concrete, but the base class is useless by itself:
所有方法都是具体的,但基类本身是无用的:
DeleteAuthor.java
删除作者.java
- Abstract class with concrete doGet method.
- doGet calls file pointed to in protectedstring sql_path.
- sql_path is null.
- 具有具体 doGet 方法的抽象类。
- doGet 调用在受保护字符串 sql_path 中指向的文件。
- sql_path 为 null。
DeleteAuthorKeepBook.java
删除作者保留Book.java
- extends abstract class DeleteAuthor
- sets sql_path to delete_author_KEEP_BOOK.sql
- 扩展抽象类 DeleteAuthor
- 将 sql_path 设置为 delete_author_ KEEP_BOOK.sql
DeleteAuthorBurnBook.java
DeleteAuthor BurnBook.java
- extends abstract class DeleteAuthor
- sets sql_path to delete_author_BURN_BOOK.sql
- 扩展抽象类 DeleteAuthor
- 将 sql_path 设置为 delete_author_ BURN_BOOK.sql
回答by bruno conde
Nice question :)
好问题:)
One thing is for sure ... this is certainly possible. The template suggestion by krosenvold is one good reason for doing this.
有一件事是肯定的……这当然是可能的。krosenvold 的模板建议是这样做的一个很好的理由。
I just want to say that a class must not be declared abstractjust for preventing it's instantiation.
我只想说一个类不能abstract仅仅为了防止它的实例化而被声明。
This is referred in the Java Language Specification Section 8.1.1.1
这在 Java 语言规范第 8.1.1.1 节中提到
回答by alepuzio
When you have an important class but the system cannot create an instance fo this class, because
当你有一个重要的类但系统无法为这个类创建实例时,因为
- this class is parent of a lot of classes of the system;
- this has a lot of responsability (methods used by a lot of class) for domain's requires;
- this class not represents a concrete object;
- 这个类是系统很多类的父类;
- 这对域的需求有很多责任(很多类使用的方法);
- 这个类不代表一个具体的对象;

