什么时候要用上Java抽象类_java – 什么时候必须使用接口而不是抽象类?
发布日期:2021-10-25 22:55:57 浏览次数:2 分类:技术文章

本文共 1061 字,大约阅读时间需要 3 分钟。

Java How to Program有关抽象类:

Because they’re used only as superclasses in inheritance hierarchies,

we refer to them as abstract superclasses. These classes cannot be

used to instantiate objects, because abstract classes are incomplete.

Subclasses must declare the “missing pieces” to become “concrete” classes,

from which you can instantiate objects. Otherwise, these subclasses, too,

will be abstract.

回答你的问题“使用interfacs的原因是什么?”:

An abstract class’s purpose is to provide an appropriate superclass

from which other classes can inherit and thus share a common design.

与接口相反:

An interface describes a set of methods that can be called on an

object, but does not provide concrete implementations for all the

methods… Once a class implements an interface, all objects of that class have

an is-a relationship with the interface type, and all objects of the

class are guaranteed to provide the functionality described by the

interface. This is true of all subclasses of that class as well.

所以,为了回答你的问题“我想知道什么时候我应该使用接口”,我想你应该使用接口,当你想要一个完整的实现和使用抽象类,当你想要的部分为你的设计(为了可重用性)

转载地址:https://blog.csdn.net/weixin_32075843/article/details/114716207 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:java编辑pdf文本_Java编辑PDF写入文字 插入图片
下一篇:java虚拟机内存面试题_从一道面试题深入了解java虚拟机内存结构

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月14日 14时54分08秒