何时使用没有接口的抽象类?
每当我创建一个抽象类时,我倾向于创建一个与之配合的接口,并让其他代码引用该接口而不是抽象类。通常,当我不创建一个接口来开始时,我会后悔(例如必须重写所有实现的方法来存根该类以进行单元测试,或者稍后新类不需要任何实现并覆盖所有内容)发现自己无法扩展任何其他课程)。
起初,我试图通过考虑 is-a 与 possible-to 来区分何时使用接口和何时使用抽象类,但我仍然会因为没有从一开始就创建接口而遭受痛苦。
那么问题是,什么时候只使用抽象类而不使用接口是一个好主意?
Whenever I create an abstract class I tend to create an interface to go along with it and have other code refer to the interface and not the abstract class. Usually when I don't create an interface to start with I regret it (such as having to override all implimented methods to stub the class for unit testing or later down the line new classes don't need any of the implimentation and override everything also finding themselves unable to extend any other class).
At first I tried to distinguish when to use an interface and when to use an abstract class by considering is-a vs able-to but I still would end up suffering later down the line for not making an interface to start with.
So the question is when is it a good idea to only have an abstract class and no interface at all?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您希望将一些基类功能“赋予”派生类,但此功能不足以实例化可用的类时,请选择抽象类。
当您希望某些类完全实现一组方法(公共契约)时,使用接口定义此类契约并通过使类继承此接口将它们强制到类上是很方便的。
简而言之:
通过抽象类,您可以为派生类提供一些通用的基本功能。除非抽象类有一些存根(必须在那里实现),否则不需要采取进一步的操作。
使用接口,您需要派生类来实现一组函数,并且不需要传递任何实现。
当您不希望强制执行任何公共契约(由接口定义的一组方法/属性)时。
此外,当您不打算使用某些编码技术时,例如将对象转换为接口类型(运行时多态性)或限制允许的输入(某些方法参数仅接受实现某些接口的类型的对象)。
When you wish to "give" some base class functionality to derived classes but when this functionality is not sufficient to instantiate a usable class, then go for abstract classes.
When you wish that some classes completely implement a set of methods (a public contract), then it is a convenient to define such contract with interfaces and enforce them onto classes by making them inherit this interface.
In short:
With abstract classes you give some common base functionality to derived classes. No further actions are necessary unless abstract class has some stubs (which have to be implemented down there).
With interfaces you require derived classes to implement a set of functions and you do not pass along any implementation.
When you do not wish to enforce any public contract (a set of methods/properties defined by an interface).
Also when you do not plan to use certain coding techniques like casting object to an interface type (run-time polymorphism) or limit allowed input (some method argument will only accept object of types which implement certain interfaces).
好吧,仅具有抽象类而没有任何接口的主要情况是标记某种类型。能够检查一个对象是否“是”某物是很有用的。这些接口将对象“标记”为某种类型。根据您使用的语言,应用不同的设计模式...
这些抽象类存在于 java 中。您还可以在 C++ 中通过 RTTI 使用它们。
Well, the main case it is useful to have only an abstract class without any interface is to mark a certain type. It is useful to be able to check if an object "is-a" something. These interface "mark" an objet to be of a certain type. Depending on the language you use, different design patterns apply ...
These sort of abstract classes exist in java. You can also use them in C++ with RTTI.