何时以及为何使用抽象类/方法?
我有一些关于抽象类/方法的基本问题。我知道抽象类的基本用途是为未来的类创建模板。但它们还有其他用途吗?什么时候你应该更喜欢它们而不是接口?什么时候不应该?另外,抽象方法什么时候有用?
I have some basic questions about abstract classes/methods. I know the basic use of abstract classes is to create templates for future classes. But are there any more uses for them? When should you prefer them over interfaces and when not? Also, when are abstract methods useful?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您不仅可以为子类定义模板,而且抽象类还提供了额外的好处,让您可以定义子类稍后可以使用的功能。
在 Java 8 之前,您无法在接口中提供默认方法实现。
如果您想向子级提供实现细节,但又不想允许直接实例化类的实例(这允许您部分定义类),则抽象类非常适合。
如果您想简单地定义对象遵循的契约,那么请使用接口。
抽象方法的用处与在接口中定义方法的用处相同。这是抽象类的设计者说“我的任何孩子都必须实现这个方法”的一种方式。
Not only can you define a template for children, but Abstract Classes offer the added benefit of letting you define the functionality that your child classes can utilize later.
You could not provide a default method implementation in an Interface prior to Java 8.
Abstract Classes are a good fit if you want to provide implementation details to your children but don't want to allow an instance of your class to be directly instantiated (which allows you to partially define a class).
If you want to simply define a contract for Objects to follow, then use an Interface.
Abstract methods are useful in the same way that defining methods in an interface is useful. It's a way for the designer of the Abstract class to say "any child of mine MUST implement this method".
阅读以下文章
http:// mycodelines.wordpress.com/2009/09/01/in-which-scenario-we-use-abstract-classes-and-interfaces/
read the following article
http://mycodelines.wordpress.com/2009/09/01/in-which-scenario-we-use-abstract-classes-and-interfaces/
在非常高的层面上:
任何类型的抽象都归结为分离关注点。抽象的“客户端”代码并不关心抽象公开的契约是如何实现的。例如,您通常不关心字符串类是否使用空终止或缓冲区长度跟踪的内部存储实现。封装隐藏了细节,但是通过创建类/方法/等。抽象,您允许更改实现或添加新的实现,而不会影响客户端代码。
At a very high level:
Abstraction of any kind comes down to separating concerns. "Client" code of an abstraction doesn't care how the contract exposed by the abstraction is fulfilled. You usually don't care if a string class uses a null-terminated or buffer-length-tracked internal storage implementation, for example. Encapsulation hides the details, but by making classes/methods/etc. abstract, you allow the implementation to change or for new implementations to be added without affecting the client code.
当类提供一些高级功能但省略了由派生类实现的某些细节时,通常使用抽象类/方法。使类/方法抽象可确保它不能单独使用,而必须专门定义高层实现中遗漏的细节。这最常与模板方法模式一起使用:
http://en.wikipedia.org/wiki/Template_method_pattern
Abstract classes/methods are generally used when a class provides some high level functionality but leaves out certain details to be implemented by derived classes. Making the class/method abstract ensures that it cannot be used on its own, but must be specialized to define the details that have been left out of the high level implementation. This is most often used with the template method pattern:
http://en.wikipedia.org/wiki/Template_method_pattern
通常,人们使用抽象类来提供一些不完整的功能,这些功能将通过具体的子类来充实。它可以提供其子类使用的方法;它还可以表示类层次结构中的中间节点,以表示具体子类的公共分组,以某种方式将它们与其超类的其他子类区分开来。由于接口不能从类派生,因此与接口相比,这是需要类(抽象或其他)的另一种情况。
一个好的经验法则是,只有类层次结构的叶节点才应该被实例化。使非叶节点抽象是确保这一点的简单方法。
Typically one uses an abstract class to provide some incomplete functionality that will be fleshed out by concrete subclasses. It may provide methods that are used by its subclasses; it may also represent an intermediate node in the class hierarchy, to represent a common grouping of concrete subclasses, distinguishing them in some way from other subclasses of its superclass. Since an interface can't derive from a class, this is another situation where a class (abstract or otherwise) would be necessary, versus an interface.
A good rule of thumb is that only leaf nodes of a class hierarchy should ever be instantiated. Making non-leaf nodes abstract is an easy way of ensuring that.