具有所有具体方法的抽象类

发布于 2024-07-09 20:35:09 字数 48 浏览 9 评论 0原文

当类中的所有方法都是具体的时,是否存在一些实际的编程情况,有人可以将类声明为抽象?

Are there some practical programming situations for someone to declare a class abstract when all the methods in it are concrete?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

过去的过去 2024-07-16 20:35:09

好吧,您可以使用模板方法模式,其中有多个覆盖点,所有覆盖点都有默认实现,但组合的默认实现本身不合法 - 任何功能实现都必须子类化。

(是的,我不喜欢模板方法模式;))

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 ;))

沉睡月亮 2024-07-16 20:35:09

抽象类是声明为抽象的类 - 它可能包含也可能不包含抽象方法。 它们无法实例化,因此如果您有一个具有具体方法的抽象类,则可以对其进行子类化,然后可以实例化该子类。

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.

攒眉千度 2024-07-16 20:35:09

想象一个接口,其声明的方法在实现时通常显示相同的默认行为。 当编写需要支持接口的类时,您必须一遍又一遍地定义所述默认行为。

为了促进具体类的实现,您可能需要提供一个抽象类,为每个方法提供默认行为。 为了支持具体类中的接口,您可以从抽象类派生并重写方法(如果它们偏离标准行为)。 这样您就可以避免重复实现相同(冗余)的默认行为。

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.

梦里人 2024-07-16 20:35:09

另一个可能的用例是装饰器,它将所有调用委托给包装的实例。 具体的装饰器实现只能重写那些添加了功能的方法:

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()");
    }
}

尽管我并不真正认为有必要将 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:

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).

始于初秋 2024-07-16 20:35:09

之前的答案已经解决了主要问题,但有一个小细节可能值得一提。

您可以有一个返回抽象类(隐藏)子类实例的工厂。 抽象类定义了结果对象上的契约,并提供了默认实现,但该类是抽象的这一事实既使其无法被直接实例化,也表明“真实”实现类的身份并不存在。发表。

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.

唐婉 2024-07-16 20:35:09

想知道为什么没有人指出 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

An abstract adapter class for receiving mouse events. The methods in
this class are empty. This class exists as convenience for creating
listener objects.

花海 2024-07-16 20:35:09

好问题:)

有一件事是肯定的......这当然是可能的。 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

◇流星雨 2024-07-16 20:35:09

当你有一个重要的类但系统无法创建该类的实例时,因为

  • 该类是系统许多类的父类;
  • 这对域的需求有很多责任(很多类使用的方法);
  • 这个类不代表一个具体的对象;

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;

Servlet 示例:

所有方法都是具体的,
但基类本身是无用的:

DeleteAuthor.java

  1. 具有具体 doGet 方法的抽象类。
  2. doGet 调用 protected 字符串 sql_path 中指向的文件。
  3. sql_path 为空

DeleteAuthorKeepBook.java

  1. 扩展抽象类DeleteAuthor
  2. 将sql_path 设置为delete_author_KEEP_BOOK.sql

DeleteAuthorBurnBook.java

  1. 扩展抽象类DeleteAuthor
  2. 将sql_path 设置为delete_author_BURN_BOOK.sql

Servlet Example:

All methods are concrete,
but the base class is useless by itself:

DeleteAuthor.java

  1. Abstract class with concrete doGet method.
  2. doGet calls file pointed to in protected string sql_path.
  3. sql_path is null.

DeleteAuthorKeepBook.java

  1. extends abstract class DeleteAuthor
  2. sets sql_path to delete_author_KEEP_BOOK.sql

DeleteAuthorBurnBook.java

  1. extends abstract class DeleteAuthor
  2. sets sql_path to delete_author_BURN_BOOK.sql
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文