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

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

问题描述

有人问我一个问题,我想在这里查看我的答案.

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

问:在哪种情况下更适合扩展抽象类而不是实现接口?

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

答:如果我们使用模板方法设计模式.

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) 我们最多可以扩展一个抽象类,但可以实现多个接口

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.

总是有一个权衡,接口给你关于基类的自由,抽象类给你以后添加新方法的自由.– 埃里希·伽玛

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.

抽象类 应该主要用于密切相关的对象.Interfaces 更擅长为不相关的类提供通用功能.

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

推荐答案

何时使用接口

接口允许某人从头开始实现您的接口或用其他一些代码实现您的接口,这些代码的原始或主要目的与您的接口完全不同.对他们来说,你的界面只是附带的,必须添加到他们的代码中才能使用你的包.缺点是接口中的每个方法都必须是公共的.您可能不想公开所有内容.

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中的抽象类与接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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