通配符和一个实现两个接口的类的问题
我有这个问题。
我有类 UserImpl 实现了 MyUser, YourUser
并且类UsersGetterImpl 实现了MyUsersGetter、YourUsersGetter
。 我想在 UsersGetterImpl
中实现一个方法,该方法返回 列表<我的用户>
,以及 MyUsersGetterinterface
的 getUsers()列出<您的用户> getUsers()
for YourUsersGetterinterface
,但我无法理解 UsersGetterImpl
类中 getUsers()
的返回类型- 可能它必须是带有通配符的东西(例如 List getUsers()
,但不完全是,因为这个例子不起作用......)
I have this question.
I have class UserImpl implements MyUser, YourUser
and class UsersGetterImpl implements MyUsersGetter, YourUsersGetter
.
I want to implement a method inside UsersGetterImpl
, which returnsList<MyUser> getUsers()
for MyUsersGetterinterface
, andList<YourUser> getUsers()
for YourUsersGetterinterface
, but I cannot understand what have to be the return type of getUsers()
inside the UsersGetterImpl
class - probably it has to be something with wildcards (like List<? extends UserImpl> getUsers()
, but not exactly, because this example won't work...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
很难说出你在问什么,但根据 Java 语言规范:
在这样的情况下:
不可能声明一个名为 getNumberOfScales 的方法,其签名和返回类型与这两个方法的签名和返回类型相同。在接口 Fish 和接口 StringBass 中声明的方法,因为一个类只能有一个具有给定签名的方法(第 8.4 节)。因此,单个类不可能同时实现接口 Fish 和接口 StringBass。
但是,如果两个接口都指定相同的返回类型,那么您可以继续实现该方法。如果
MyUser
和YourUser
有一个共同的祖先,那么你可以做List
或者如果它们没有通用性,您可以简单地使用List。
但此时,您必须停下来考虑一个通用实现是否是您实际需要的想。我怀疑如果您向我们提供有关您的问题的更多详细信息,可能会有更优雅的解决方案。
编辑:
根据您的评论,您想要类似的东西...
这是未经测试的,我猜有 50% 的工作机会。
架构建议是,您实际上可能需要一个接口的两个实现,而不是两个接口的单一实现:
It is hard to tell what you are asking, but according to the Java Language Specification:
In a situation such as this:
It is impossible to declare a method named getNumberOfScales with the same signature and return type as those of both the methods declared in interface Fish and in interface StringBass, because a class can have only one method with a given signature (§8.4). Therefore, it is impossible for a single class to implement both interface Fish and interface StringBass.
However, if both of your interfaces specify the same return type, then you can go ahead and implement that method. If
MyUser
andYourUser
have a common ancestor then you could doList<? extends User>
or if they have no commonality you can use simply useList<?>.
At that point though, you have to stop and consider if a common implementation is what you actually want. I suspect there may be a more elegant solution, if you provided us with more details about your problem.
Edit:
Based on your comment, you want something like...
This is untested, and I'd guess has a 50% chance of working.
The architectural suggestion is that instead of having a single implementation for two interfaces you might actually want two implementations of one interface:
简短的回答是:这是不可能的。您不能拥有两个签名仅因返回类型不同而不同的方法,因此不能让一个类实现两个定义仅因返回类型不同的方法的接口。
简单的解决方法是使 MyUsersGetter 和 YourUsersGetter 具有不同名称的方法。
一种可能的解决方法是让 UsersGetterImpl 不直接实现 MyUsersGetter 和 YourUsersGetter,而是使用委托:
The short answer is: it can't be done. You cannot have two methods whose signature differs only by return type, and you therefore cannot have one class implement two interfaces that define methods that only differ by return type.
The easy fix is to make MyUsersGetter and YourUsersGetter have methods with different names.
One possible workaround would be to have UsersGetterImpl not implement MyUsersGetter and YourUsersGetter directly, but to have delegates: