Java中抽象类的目的是什么?

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

What is the purpose of abstract classes in Java?

javaoopabstract-class

提问by Spiky

I have some questions around abstract classesand their use. I know the basics about them; for example, that they can't be instantiated, they can have concrete and abstract methods, ... But I guess what I wanna know is, what purpose do they serve in life (in software engineering)?

我对抽象类及其使用有一些疑问。我了解它们的基础知识;例如,它们不能被实例化,它们可以有具体和抽象的方法,......但我想我想知道的是,它们在生活中(在软件工程中)有什么用途?

What is the purpose of abstract classes in Java? Why and when should one use an Abstract class? If you can use a normal class and then inherit it, why would you inherit an abstract class? How would using abstract classes make our life easier? would it provide better maintainability? more flexibility? ...

Java中抽象类的目的是什么?为什么以及何时应该使用抽象类?如果你可以使用一个普通的类然后继承它,你为什么要继承一个抽象类?使用抽象类如何让我们的生活更轻松?它会提供更好的可维护性吗?更灵活?...

btw, I've already looked at some similar questions and answers, including Understanding the purpose of Abstract Classes in Java, but they don't answer my question. I'm more looking for answers that shed light on the philosophy behind the introduction of abstract classes in the first place in Java.

顺便说一句,我已经看过一些类似的问题和答案,包括了解 Java 中抽象类的目的,但他们没有回答我的问题。我更多的是在寻找能够阐明 Java 中首先引入抽象类背后的哲学的答案。

Thanks

谢谢

采纳答案by OPK

An abstractclass can be used as a type of template for other classes, and most commonly used for inheritance.

一个abstract类可以用作其他类的模板类型,最常用于inheritance.

The best example is from the book head first java:

最好的例子是从书头第一个java:

abstract class Animal(){}

then you can extend child class such as: dog, cat, fish.

然后您可以扩展子类,例如:dog, cat, fish