在Java中,当一个接口扩展另一个接口时,为什么要重新声明子接口中的方法? [英] In Java when one interface extends another, why would one redeclare a method in a subinterface?

查看:204
本文介绍了在Java中,当一个接口扩展另一个接口时,为什么要重新声明子接口中的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在查看J2EE中的JMS API,并发现了一个奇怪的行为,其中在接口中声明的某些方法(例如,Session中的createQueue)在子接口(例如QueueSession)中再次声明,并且具有相同的文档。

I've been looking at the JMS API from J2EE and found a strange behavior where certain methods that are declared in an interface (e.g., createQueue in Session) are declared again in the subinterfaces such as QueueSession, and with identical documentation.

由于子接口继承它继承的接口的所有方法声明,并且因为JavaDoc工具没有问题整理子接口的JavaDoc并创建继承操作列表,我无法弄清楚它实现了什么。

Since an subinterface "inherits" all the method declarations of the interface it inherits, and since the JavaDoc tool has no problem sorting out the JavaDocs of the subinterface and creating an "inherited operations" list, I can't figure out what this achieves.

唯一的想法是,最初调用是在Session中,然后在调用时移到QueueSession创建了特定的子类,但是我希望在大写的文档中看到一些东西。但这只是猜想。

The only think of is that initially the call was in Session, and then moved to QueueSession when the specific subclass was created, though then I would have expected to see something in the documentation of the upperclass. But this is just conjecture.

所以问题是:
是否有令人信服的理由在子接口中重新声明方法?

So the question is: Is there a convincing reason to redeclare a method in a subinterface?

推荐答案

在为Sun工作时偶尔看到这种情况,我可以告诉你它是如何发生的。有人用一些方法定义了一个接口,比如Alice。很多开发人员实现了这个界面。

Having seen this happen on occasion while working for Sun, I can tell you how it usually happens. Someone defines an interface, let's say Alice, with some methods; many developers implement that interface.

一段时间之后,它意识到他们需要一些其他的接口,称之为Bob,它有Alice的一部分方法,按顺序排列允许它作为另一个接口的基本接口,Clara。

Some time later, it's realized that they need some other interface, call it Bob, that has a subset of the methods of Alice, in order to allow it to serve as a base interface for another interface, Clara.

如果你将Alice的方法移动到Bob中,你就会破坏所有实现Alice的代码;你必须回去并至少重新编译一大堆代码,其中一些你可能不拥有,并且出于政治原因不能破解。

If you move the methods of Alice into Bob, you break all the code that implements Alice; you have to go back and at least recompile a whole bunch of code, some of which you may not own, and for political reasons can't break.

所以你不要't。

这篇关于在Java中,当一个接口扩展另一个接口时,为什么要重新声明子接口中的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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