java - 抽象类和具体类之间的独特区别

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

java - unique difference between abstract class and concrete class

java

提问by user826323

I know few differences between abstract class and concrete class. I know that you can't create an instance with abstract class unlike concrete class, abstract class can have 'abstract' methods.

我知道抽象类和具体类之间几乎没有区别。我知道与具体类不同,您不能使用抽象类创建实例,抽象类可以具有“抽象”方法。

But i have an example like the following. A lot of times, we see the following examples at work. I will just skip some common methods that can be defined in the Parent class.

但我有一个像下面这样的例子。很多时候,我们会在工作中看到以下示例。我将略过一些可以在 Parent 类中定义的常用方法。

public abstract class Parent {
    public void init() {
       doInit();
    }
    public abstract void doInit();
}

public class Child extends Parent {
    public void doInit() {
       // implementation
    }
}

I think that we can do the same thing with a concrete class like the following.

我认为我们可以用像下面这样的具体类来做同样的事情。

public class Parent {
    public void init() {
      doInit();
    }
    public void doInit() {
       // Empty
    }
}

I am curious to see if there is any unique situation that we have to use abstract class. Is there any significant difference during runtime with the above example?

我很好奇,看看有没有什么特殊情况我们必须使用抽象类。上面的例子在运行时有什么显着差异吗?

Thank you.

谢谢你。

回答by dasblinkenlight

The reason to use abstractclass in this situation is to force everyone inheriting your base class to override the abstract doInitmethod. Without the class and the method being abstract, they may forget to do so, and the compiler would not catch them.

abstract在这种情况下使用class的原因是强制每个继承你的基类的人覆盖抽象doInit方法。如果类和方法不是抽象的,他们可能会忘记这样做,编译器不会捕获它们。

In addition to this pragmatic purpose, abstract classes provide a powerful way to communicate your design idea to the readers of your code. An abstract class tells the reader that the methods inside provide some common implementation for a group of related classes, rather than implementing a single concept that you are modeling. Very often communicating your intent to your readers is as important as it is to write correct code, because otherwise they might break something while maintaining your code.

除了这个实用目的之外,抽象类还提供了一种强大的方式来将您的设计理念传达给您的代码读者。抽象类告诉读者,其中的方法为一组相关类提供了一些通用实现,而不是实现您正在建模的单个概念。通常,将您的意图传达给您的读者与编写正确的代码一样重要,否则他们可能会在维护您的代码时破坏某些内容。

It is customary in Java to call abstract classes Abstract...; in your example that would be AbstractParent.

Java 中习惯于调用抽象类Abstract...;在您的示例中,将是AbstractParent.

回答by Cajetan Rodrigues

Of course you can do it that way, but it all depends on the right business logic.There might arise a situation where you'd want to enforce a policy on people extending your code.

当然,您可以这样做,但这完全取决于正确的业务逻辑。可能会出现一种情况,您希望对扩展代码的人实施策略。

For example, I write an Employee class and you extend my class for writing a ProjectManager class. But suppose the business does not allow direct instantiation of Employee (like I said, just an example). So I declare my Employee class as abstract, thereby enforcing upon all extenders (read:you) of my class the rule that they can't instantiate Employee directly. (It will happen indirectly through the inheritance chain, of course, i.e. parent objects are created before child objects.)

例如,我编写了一个 Employee 类,而您扩展了我的类来编写一个 ProjectManager 类。但是假设业务不允许直接实例化 Employee (就像我说的,只是一个例子)。所以我将我的 Employee 类声明为抽象类,从而对我的类的所有扩展程序(阅读:你)强制执行他们不能直接实例化 Employee 的规则。(当然,它会通过继承链间接发生,即在子对象之前创建父对象。)

Used properly, a person at place A controls how another person at place B will code.

如果使用得当,A 地的一个人控制 B 地的另一个人将如何编码。

回答by Kshitish

A concrete class is one which has implementation (code inside) for all the methods. It does not matter whether it is derived from some other class.

具体类是具有所有方法的实现(内部代码)的类。它是否派生自其他类并不重要。

public abstract class IAmAbstract{
    public void writeMe(){
        System.out.println("I am done with writing");
    }
}

public class IAmConcrete extends IAmAbstract{
    public void writeMe(){
        System.out.println("I am still writing");
    }
} 

回答by Aatch

Abstract classes have a variety of useful properties in use with software design.

抽象类在软件设计中具有多种有用的属性。

Other than the obvious differences, such as being unable to be instantiated and being able to hold abstract methods. They are useful for defining common, yet overridable, functions, holding static methods that deal with it's children in a logical manner.

除了明显的区别,例如无法实例化和能够持有抽象方法。它们可用于定义通用但可覆盖的函数,保存以逻辑方式处理其子项的静态方法。

My favorite is the abstract factory pattern though.

不过,我最喜欢的是抽象工厂模式。

By making a factory that is the parent of all the classes it may create, it can force functionality required for creation, this actually causes an odd artefact where technically tighter-coupled code is actually easier to maintain.

通过创建一个作为它可能创建的所有类的父类的工厂,它可以强制创建所需的功能,这实际上会导致一个奇怪的人工制品,其中技术上更紧密耦合的代码实际上更容易维护。