问题描述
我一直在查看 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 的方法的子集,以便允许它作为另一个的基本接口界面,克拉拉.
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.
所以你没有.
这篇关于在 Java 中,当一个接口扩展另一个接口时,为什么要在子接口中重新声明方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!