Java 抽象类扩展了具体类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20970980/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:38:55  来源:igfitidea点击:

Abstract class extends concrete class

javadesign-patterns

提问by Heisenberg

I earlier learned that abstract class can extend concrete class. Though I don't see the reason for it from JAVA designers, but it is ok. I also learned that abstract class that extends concrete class can make overriden methods abstract. Why? Can you provide with use case where it is useful? I am trying to learn design patterns and I do not want to miss anything.

我之前了解到抽象类可以扩展具体类。虽然我没有从 JAVA 设计师那里看到原因,但没关系。我还了解到扩展具体类的抽象类可以使覆盖的方法抽象。为什么?你能提供有用的用例吗?我正在尝试学习设计模式,我不想错过任何东西。

Here is example:

这是示例:

public class Foo 
{
    public void test()
    {
    }
}

public abstract class Bar extends Foo
{
   @Override
   public abstract void test();
}

采纳答案by Michael Berry

This becomes useful if I have a set of classes that I want a default implementation of test()for (so they can extend from Foo), and a subset of those classes that I want to force to provide their own implementation (in which case making it abstract in the subclass would enforce this.)

如果我有一组类,我想的默认实现这成为有用test()的(这样他们就可以从扩展Foo),以及这些类别的子集,我想以武力提供自己的实现(在这种情况下,使它摘要子类将强制执行此操作。)

Of course, the alternative way in this example would be to declare test()abstract in the top level class rather than in the subclass, and that's what you'd usually do - but there are cases where satisfying the is-a relationship of inheritance means that it occasionally makes more sense from a design perspective doing it this way around. It's rare, but you do sometimes see it.

当然,这个例子中的另一种方法是test()在顶级类而不是在子类中声明抽象,这就是你通常会做的 - 但在某些情况下,满足 is-a 继承关系意味着它从设计的角度来看,这样做有时更有意义。这种情况很少见,但有时确实会看到。

As an aside, though a special case, remember that all classes implicitly extend Objectunless otherwise specified. So if you include this case, abstract classes extending concrete classes isn't so unusual after all!

顺便说一句,尽管是一种特殊情况,请记住,Object除非另有说明,否则所有类都隐式扩展。因此,如果您将这种情况包括在内,那么扩展具体类的抽象类毕竟并不少见!

回答by Nageswara Rao

To make the class that extended your class (made abstract) to provide specific type of implementation.

使扩展您的类(抽象)的类提供特定类型的实现。

For example:

例如:

  • abstract ClassA
  • ClassB extends ClassB - Provides specific implementation of abstract methods defined in ClassA
  • 抽象类A
  • ClassB 扩展 ClassB - 提供 ClassA 中定义的抽象方法的特定实现

回答by Benjamin Gale

If you make the testmethod abstract it forces anyone deriving from the Barclass provide an implementation of the method.

如果您使该test方法抽象,它会强制从Bar该类派生的任何人提供该方法的实现。

If you remove the abstract method from the Barclass then anyone deriving from Barwould not haveto implement the testmethod as Fooalready provides an (empty) implementation.

如果从删除抽象方法Bar类,那么任何人获得Bar不会实现test的方法Foo已经提供了一个(空)的实现。

回答by Janakiram

It is basically to selectively reuse some existing (legacy?) code.

基本上是选择性地重用一些现有(遗留?)代码。

For example: Suppose someone has already created a concrete-class-C(of course the complete implementation).

例如:假设有人已经创建了一个具体的类-C(当然是完整的实现)。

Now, since you are designing a new system (which has an abstract class-A) and you analyzed the existing system and found that you are going to have some methods that are almost similar to the methods of concrete-class-C. But you also found that some methods of the concrete class-Care too specific and you want to enforce implementation of those methods in the concrete-subclasses of the abstract class-A.

现在,由于您正在设计一个新系统(它有一个抽象类 A)并且您分析了现有系统并发现您将拥有一些与具体类 C的方法几乎相似的方法。但是您还发现具体类 C 的某些方法过于具体,您希望在抽象类 A的具体子类中强制实现这些方法。

Thus, it enables you selectively choose what methods to re-use and what not to reuse.

因此,它使您能够有选择地选择要重用的方法和不重用的方法。

回答by FatherMathew

This flexibility is especially useful when a system evolves and we don't want to disturb the existing code.

当系统发展并且我们不想干扰现有代码时,这种灵活性特别有用。

A simple example I could think of is consider a MSDocReaderclass. This class is part of the legacy system and many other applications depend on this.

我能想到的一个简单例子是考虑一个MSDocReader类。此类是遗留系统的一部分,许多其他应用程序都依赖于此。

Now the requirements change. We have to write classes for reading docx files, ppt and even xsl files.

现在要求发生了变化。我们必须编写用于读取 docx 文件、ppt 甚至 xsl 文件的类。

The MSDocReader class contains methods which can be reused such as getting the size of the file in KBs, onnecting to the .Net framework (If I'm not wrong :-))

MSDocReader 类包含可以重用的方法,例如以 KB 为单位获取文件的大小,连接到 .Net 框架(如果我没记错的话 :-))

Now using this provision, we can write an absract class AbstractMSFileReaderwhich will contain all the methods that are used in the MSDocReader. But this class will have the read method as abstract.

现在使用这个规定,我们可以编写一个抽象AbstractMSFileReader,它将包含MSDocReader中使用的所有方法。但是这个类会将 read 方法作为抽象方法。

The reason being we want to force the developers to use their own version of read method. (We should not use inheritance as the inheritance clearly states that the subclass method will extend the functionality of the parent method. Reading a doc file and reading a excel file are 2 different things and they do not fall under the same hierarchy. )

原因是我们要强制开发人员使用他们自己版本的 read 方法。(我们不应该使用继承,因为继承明确指出子类方法将扩展父方法的功能。读取 doc 文件和读取 excel 文件是 2 件不同的事情,它们不属于同一层次结构。)

And you may argue that the we can make an abstract class and make the MSDocReaderclass extend that abstract class. But it can happen that the MSDocReaderclass may be extending some other class and since java does not support multiple inheritance., it can create problems

您可能会争辩说,我们可以创建一个抽象类并使MSDocReader类扩展该抽象类。但是, MSDocReader类可能会扩展某个其他类,并且由于 java 不支持多重继承,因此可能会产生问题