Java中抽象类的目的是什么? [英] What is the purpose of abstract classes in Java?

查看:434
本文介绍了Java中抽象类的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some questions around abstract classes and 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)?

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

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

顺便说一句,我已经看过一些类似的问题和答案,包括

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.

谢谢

推荐答案

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

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

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

The best example is from the book head first java:

abstract class Animal(){}

然后您可以扩展子类,例如:dogcatfish.

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

这篇关于Java中抽象类的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆