java 为什么父类和子类都实现相同的接口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5668429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:11:39  来源:igfitidea点击:

Why would both a parent and child class implement the same interface?

javainheritanceinterface

提问by Riggy

I inherited some legacy Java (1.4) code and this design decision appears regularly. I can't understand if there's any purpose or reason to it.

我继承了一些遗留的 Java (1.4) 代码,这个设计决定经常出现。我不明白这样做是否有任何目的或理由。

public interface SoapFacade extends iConfigurable{ }

public class SoapFacadeBase implements SoapFacade{
...
}

public class SoapFacadeImpl extends SoapFacadeBase implements SoapFacade{
...
}

As I understand interfaces (and my experimentation has reinforced), there is no purpose to having both the parent and the child implement the same interface. In this scenario, everything from SoapFacadeis implemented in SoapFacadeBase, but the method in iConfigurableis implemented in SoapFacadeImpl. However, that doesn't create a need to have SoapFacadeImplimplement SoapFacade.

根据我对接口的理解(并且我的实验得到了加强),让父级和子级都实现相同的接口是没有意义的。在这种情况下,fromSoapFacade中的所有内容都在中实现SoapFacadeBase,但 in 中的方法在iConfigurable中实现SoapFacadeImpl。但是,这并不需要SoapFacadeImpl实现SoapFacade

Is there something I don't know about interfaces that would give this pattern some purpose or benefit? Are there underlying costs beyond lack of clarity that should drive refactoring it? Or should it simply be refactored for clarity/simplicity?

有什么我不知道的关于接口的东西会给这个模式带来一些目的或好处吗?除了缺乏清晰性之外,是否还有潜在的成本来推动重构?还是应该为了清晰/简单而简单地重构它?

回答by aioobe

As I understand interfaces (and my experimentation has reinforced), there is no purpose to having both the parent and the child implement the same interface.

根据我对接口的理解(并且我的实验得到了加强),让父级和子级都实现相同的接口是没有意义的。

No. Technically, it is completely redundant.

不。从技术上讲,它是完全多余的。

It doeshowever document the fact that you intend SoapFacadeImplto be a SoapFacadeand it ensures that you get a compile error, if you (or someone else) decides to remove implements SoapFacadefrom the base class.

但是,它确实记录了您打算SoapFacadeImpl成为 a的事实,SoapFacade并且如果您(或其他人)决定implements SoapFacade从基类中删除,它可以确保您收到编译错误。

You see this pattern everywhere in the standard Java Collections API. ArrayListimplements Listeven though its base class (AbstractList) already, does. Same holds for HashSet/ AbstractSetand the Setinterface.

在标准 Java 集合 API 中随处可见这种模式。ArrayList工具List虽然其基类(AbstractList)已经,确实。同样适用于HashSet/AbstractSetSet接口。

回答by Howard

If you use the interface also as a marker. Class.getInterfaces();will only return directly instanced interfaces.

如果您使用界面也作为标记。Class.getInterfaces();只会返回直接实例化的接口。

回答by gd1

I actually find that design pointless. Implemented interfaces, as you stated, are just inherited, so there's no need to copy and paste "implements SomeInterface" on the children classes. It's not clearer, smarter, or whatsoever...

我实际上发现这种设计毫无意义。正如您所说,实现的接口只是继承的,因此无需在子类上复制和粘贴“实现 SomeInterface”。它不是更清晰,更智能,或任何......

回答by irreputable

It is nonsense, don't do it.

胡说八道,别做。

Especially in a public API like java collections. It's absolutely nonsense.

特别是在像 java 集合这样的公共 API 中。这绝对是胡说八道。