Java中的抽象类vs接口 [英] Abstract class vs Interface in Java

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

问题描述

我被问到一个问题,我想在这里回答我的答案。

I was asked a question, I wanted to get my answer reviewed here.

Q:在哪种情况下,扩展抽象类而不是实现接口?

Q: In which scenario it is more appropriate to extend an abstract class rather than implementing the interface(s)?

A:如果我们使用模板方法设计模式。

A: If we are using template method design pattern.

我是否正确?

如果我无法清楚地说明问题,我很抱歉。

我知道抽象类和接口之间的基本区别。

I am sorry if I was not able to state the question clearly.
I know the basic difference between abstract class and interface.

1)当需求使用抽象类时,我们需要在每个子类中实现相同的功能具体操作(实现方法)和其他操作的不同功能(仅限方法签名)

1) use abstract class when the requirement is such that we need to implement the same functionality in every subclass for a specific operation (implement the method) and different functionality for some other operations (only method signatures)

2)如果需要将签名相同,使用界面(和实现不同),以便您可以遵守接口实现

2) use interface if you need to put the signature to be same (and implementation different) so that you can comply with interface implementation

3)我们可以扩展一个抽象类的最大值,但可以实现m比一个界面更方便

3) we can extend max of one abstract class, but can implement more than one interface

重新提出问题:除了上述提到的其他情况之外,还有其他场景,具体我们需要使用抽象类(一个是看到的是模板方法设计模式在概念上仅基于这个)?

Reiterating the question: Are there any other scenarios, besides those mentioned above, where specifically we require to use abstract class (one is see is template method design pattern is conceptually based on this only)?

接口与抽象类

这两者之间的选择真的取决于你想做什么,但对我们来说幸运的是,Erich Gamma可以帮助我们。

Choosing between these two really depends on what you want to do, but luckily for us, Erich Gamma can help us a bit.

和往常一样,一个接口让你对基类有自由,一个抽象类可以让你添加新方法的自由。 - Erich Gamma

As always there is a trade-off, an interface gives you freedom with regard to the base class, an abstract class gives you the freedom to add new methods later. – Erich Gamma

您无法在代码中更改界面而无需更改其他许多,因此唯一的避免这种情况的方法是创建一个全新的界面,这可能并不总是一件好事。

You can’t go and change an Interface without having to change a lot of other things in your code, so the only way to avoid this would be to create a whole new Interface, which might not always be a good thing.

抽象类应主要用于密切相关的对象。 接口更适合为不相关的类提供通用功能。

Abstract classes should primarily be used for objects that are closely related. Interfaces are better at providing common functionality for unrelated classes.

推荐答案

strong>何时使用界面

When To Use Interfaces

一个界面允许有人从头开始实现您的界面,或者在其原始或主要目的的其他代码中实现您的界面与您的界面完全不同对他们而言,您的界面只是偶然的,必须添加到他们的代码才能使用您的包。缺点是接口中的每个方法都必须是public。您可能不想公开所有内容。

An interface allows somebody to start from scratch to implement your interface or implement your interface in some other code whose original or primary purpose was quite different from your interface. To them, your interface is only incidental, something that have to add on to the their code to be able to use your package. The disadvantage is every method in the interface must be public. You might not want to expose everything.

何时使用抽象类

相比之下,抽象类提供了更多的结构。它通常定义一些默认实现,并提供一些有用的完整实现工具。捕获是,使用它的代码必须使用你的类作为基础。如果其他程序员想要使用您的程序包已经独立开发了自己的类层次,这可能会非常不方便。在Java中,一个类可以只继承一个基类。

An abstract class, in contrast, provides more structure. It usually defines some default implementations and provides some tools useful for a full implementation. The catch is, code using it must use your class as the base. That may be highly inconvenient if the other programmers wanting to use your package have already developed their own class hierarchy independently. In Java, a class can inherit from only one base class.

何时使用

您可以提供两个世界中最好的一个接口和一个抽象类。如果他们选择,实现者可以忽略你的抽象类。这样做的唯一缺点是通过它们的接口名调用方法比通过抽象类名称调用方法慢一些。

You can offer the best of both worlds, an interface and an abstract class. Implementors can ignore your abstract class if they choose. The only drawback of doing that is calling methods via their interface name is slightly slower than calling them via their abstract class name.

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

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