本文共 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!