Java 接口:继承、重写和重载方法
在 Ken Arnold、James Gosling、David Holmes 所著的《THE Java™ 编程语言,第四版》中,提到:
段落:(4.3.2) “同样,如果一个接口继承了多个具有相同签名的方法,或者一个类实现了包含具有相同签名的方法的不同接口,则只有一个这样的方法。该方法的实现最终由实现接口的类,并且那里没有歧义。如果方法具有相同的签名但返回类型不同,则其中一个返回类型必须是所有其他返回类型的子类型,否则会发生编译时错误。必须定义一个返回该公共子类型的方法。”
任何人都可以给我一些示例代码来证明上一段的观点吗?
我尝试编写代码并测试所提到的内容,但我我收到编译时错误 子接口隐藏了基接口方法,因此只能实现子接口方法。
提前致谢。 -阿伦
In "THE Java™ Programming Language, Fourth Edition" By Ken Arnold, James Gosling, David Holmes, its mentioned that:
paragraph: (4.3.2)
"Similarly, if an interface inherits more than one method with the same signature, or if a class implements different interfaces containing a method with the same signature, there is only one such method. The implementation of this method is ultimately defined by the class implementing the interfaces, and there is no ambiguity there. If the methods have the same signature but different return types, then one of the return types must be a subtype of all the others, otherwise a compile-time error occurs. The implementation must define a method that returns that common subtype."
Can anybody give me some example code that justifies the points of above paragraph ?
I tried to write the code and test what is mentioned but I am getting compile-time error
the sub-interface hides the base interface method so can only implement sub-interface method.
Thanks in advance.
-Arun
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如您所见,
Impl.method()
实现了A.method()
和B.method()
,而Impl .returnMethod()
返回一个B
,它是Object
的子级,从而履行A.returnMethod()
的契约也。 后者是否需要一个不是B.returnMethod()
的返回类型的父级的返回类型,这将是一个编译错误,因为Impl
。As you can see,
Impl.method()
implements bothA.method()
andB.method()
, whileImpl.returnMethod()
returns aB
, which is a child ofObject
, thus fulfillingA.returnMethod()
's contract too. Would the latter require a return type that is not a parent ofB.returnMethod()
's return type that would be a comile error, since no such implementation could exist inImpl
.在以下两个接口中,
methodA()
在参数(无)和返回类型(int)方面的定义相同。 底部的实现类定义了具有此精确签名的单个方法。 由于它符合两个接口,因此不会有任何问题 - 通过 InterfaceA 或 InterfaceB 类型的引用进行的任何调用都将分派到此实现。第二个
methodB()
定义为返回InterfaceA
中Number
的任何子类型(或Number
本身)。InterfaceB
将methodB()
定义为返回Integer
,它是Number
的子类型。 实现类实际上用Integer
实现该方法,从而遵守InterfaceA
和InterfaceB
的约定。 这里也没有问题。然而,被注释掉的
methodB()
被实现为返回Double
的情况是行不通的:虽然它可以满足InterfaceA
的约定,但它会与InterfaceB
(需要一个Integer
)冲突。如果
InterfaceA
和InterfaceB
还为methodC()
指定(不同的)契约(在示例中注释掉),这将是矛盾的并创建编译器错误。 Java 中不允许同时实现这两个签名(仅返回类型不同)。如果向方法添加任何参数,上述规则也适用。 为了简单起见,我将其保留在示例中。
In the following two interfaces
methodA()
is identically defined in terms of parameters (none) and return type (int). The implementation class at the bottom defines a single method with this exact signature. As it complies to both interfaces, you get no problem there - any calls made via a reference of type InterfaceA or InterfaceB will be dispatched to this implementation.The second
methodB()
is defined as returning any subtype ofNumber
(orNumber
itself) inInterfaceA
.InterfaceB
definesmethodB()
as returning anInteger
which is a subtype ofNumber
. The implementation class actually implements the method withInteger
, thus complying to the contract of bothInterfaceA
andInterfaceB
. No problem here either.The commented out case of
methodB()
being implemented as returning aDouble
however would not work: While it would satisfy the contract ofInterfaceA
, it would conflict withInterfaceB
(which demands anInteger
).If
InterfaceA
andInterfaceB
were also specifying (different) contracts for amethodC()
(commented out in the example) this would be contradictory and create a compiler error. Implementing both signatures (differing only in return type) is not allowed in Java.The above rules would also hold true if were to add any parameters to the methods. For simplicity I kept this out of the example.
你的意思是这个吗?:
MyClass中的这样一个方法是由javac自动生成的,作为合成桥方法。 您必须实现一个返回与所有已实现/覆盖的方法兼容的类型的方法(在本例中为
Number
/Integer
/Double
/etc )。Is this what you mean?:
Such a method in MyClass is automatically generated by javac as a synthetic bridge method. You must implement a single method returning a type compatible with all of the implemented/overridden methods (in this case
Number
/Integer
/Double
/etc).