为什么我们需要 Java 中的抽象类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22085985/
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
why do we need abstract classes in Java?
提问by user3314801
Why do we need abstract classes in Java? If you're never going to make it into an object, why have it in the first place? How do you use it? Why is it there? I'm wondering the same thing with abstract methods. I find it seems like a similar concept to having a super class with NO subclasses that could ever matter to be made.
为什么我们需要 Java 中的抽象类?如果你永远不会把它变成一个对象,为什么首先要拥有它?你如何使用它?为什么会在那里?我想知道抽象方法也有同样的事情。我发现这似乎是一个类似的概念,即拥有一个没有可能重要的子类的超类。
回答by Scary Wombat
An abstract class can be used as a type of template for other classes. The abstract class will hold common functionality for all classes that extend it.
抽象类可以用作其他类的模板类型。抽象类将拥有扩展它的所有类的通用功能。
For example:
例如:
Abstract Class Animal
All animals move and breathe and reproduce so these can be put into the Animal Class.
所有动物都会移动、呼吸和繁殖,因此可以将它们放入动物类。
Now
现在
Concrete Class Dog, Cat etc.
Have these base functions already provided.
已经提供了这些基本功能。
回答by keshlam
Abstract classes permit providing a partialset of default implementations of methods in a class. Since they're incomplete, they can't be instantiated and used as they stand, but they can be subclassed to add the missing details in a way that's specific to that particular implementations, and those subclasses can be instantiated.
抽象类允许提供类中方法的部分默认实现。由于它们是不完整的,因此它们不能被实例化并按原样使用,但它们可以被子类化,以特定于特定实现的方式添加缺少的细节,并且可以实例化这些子类。
Without abstract classes, you would have to provide dummy implementations of the methods you intend to override ... which could be done, but then there'd be the risk of forgetting to implement one of them. Having some methods remain entirely abstract ensures that the real implementations have to fill in the gaps, or continue to be abstract themselves and force their descendents to do so.
如果没有抽象类,您将不得不提供您打算覆盖的方法的虚拟实现......这是可以完成的,但是会有忘记实现其中之一的风险。让一些方法保持完全抽象可以确保真正的实现必须填补空白,或者继续抽象自己并迫使他们的后代这样做。
It's not something the language couldn't live without. But it's Very Useful. You'll discover just how useful as you become more proficient in Java and OO design.
这不是语言不能没有的东西。但它非常有用。当您越来越精通 Java 和 OO 设计时,您会发现它是多么有用。
(Note that we said the same thing, basically, last time you brought up this question. So if you're still confused, you might want to be more specific about exactly what's confusing you.)
(请注意,基本上,上次您提出这个问题时,我们说了同样的话。因此,如果您仍然感到困惑,您可能想要更具体地说明究竟是什么让您感到困惑。)
回答by Harish Talanki
The are many uses of abstract clasees, the main purpose of abstract classes is to function as base classes which can be extended by subclasses to create a full implementation.
抽象类的用途很多,抽象类的主要目的是充当基类,可以由子类扩展以创建完整的实现。
For example,
例如,
You may have three steps to be implemented in your program,
您可能需要在您的程序中实现三个步骤,
- Few steps before the action
- Some action to be performed
- Few steps after the action
- 行动前的几步
- 要执行的一些操作
- 动作后几步
So in this case you can define an abstract class with the three methods like this:
因此,在这种情况下,您可以使用以下三个方法定义一个抽象类:
public abstract MyAbstractProcess {
public void stepBefore() {
//implementation directly in abstract superclass
}
public abstract void action(); // implemented by subclasses
public void stepAfter() {
//implementation directly in abstract superclass
}
}
Also, the above example of abstract class Animal is also a very good example.
另外,上面抽象类Animal的例子也是一个很好的例子。