原文地址:https://www.cnblogs.com/estrigriac/p/11257455.html
问题:如果有两个接口,他们恰好声明了同名方法,而这两个方法的参数相同,但返回类型不同。如何写一个类,同时实现这两个接口?
答:没法实现。这篇文章[1]已经给出证明了,就不再累述。
关于如何绕开这个问题,大致有两种方案。
一是声明一个同时实现两个返回类型的类[2][3],二是使用两个inner class分别继续这两个接口[4]。个人感觉区别不大,看情况使用即可。但第一个方案在处理继承多个类时行不通,而且处理void也很麻烦。
C#就很好地解决了这个问题。C#允许一个类在实现接口时,显式地标注实现的是哪个接口的哪个方法[5]。
参考资料
- https://www.geeksforgeeks.org/two-interfaces-methods-signature-different-return-types/
- https://stackoverflow.com/questions/31562741/two-interface-have-same-method-name-with-different-return-type
- https://coderanch.com/t/402880/java/methods-signature-return-types-interfaces
- https://stackoverflow.com/questions/31562741/two-interface-have-same-method-name-with-different-return-type
- https://www.geeksforgeeks.org/c-sharp-how-to-implement-multiple-interfaces-having-same-method-name/
本文探讨了当两个接口拥有同名方法但返回类型不同时,如何在Java中实现这些接口的问题。提出了两种解决方案:创建一个实现两种返回类型的类或使用内部类分别继承接口。并介绍了C#如何通过明确指定实现的方法来解决此问题。
1620

被折叠的 条评论
为什么被折叠?



