通配符和一个实现两个接口的类的问题

发布于 2024-09-09 02:43:41 字数 508 浏览 4 评论 0原文

我有这个问题。
我有类 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 returns
List<MyUser> getUsers() for MyUsersGetterinterface, and
List<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何以心动 2024-09-16 02:43:41

很难说出你在问什么,但根据 Java 语言规范

在这样的情况下:

interface Fish { int getNumberOfScales(); }
interface StringBass { double getNumberOfScales(); }
class Bass implements Fish, StringBass {
    // This declaration cannot be correct, no matter what type is used.
    public ??? getNumberOfScales() { return 91; }
}

不可能声明一个名为 getNumberOfScales 的方法,其签名和返回类型与这两个方法的签名和返回类型相同。在接口 Fish 和接口 StringBass 中声明的方法,因为一个类只能有一个具有给定签名的方法(第 8.4 节)。因此,单个类不可能同时实现接口 Fish 和接口 StringBass。

但是,如果两个接口都指定相同的返回类型,那么您可以继续实现该方法。如果 MyUserYourUser 有一个共同的祖先,那么你可以做 List 或者如果它们没有通用性,您可以简单地使用 List。

但此时,您必须停下来考虑一个通用实现是否是您实际需要的想。我怀疑如果您向我们提供有关您的问题的更多详细信息,可能会有更优雅的解决方案。

编辑:

根据您的评论,您想要类似的东西...

interface MyUserGetter { List<? extends MyUser> getUsers(); }
interface YourUserGetter { List<? extends YourUser> getUsers(); }
class UserGetterImpl { List<? extends UserImpl> getUsers(); }

这是未经测试的,我猜有 50% 的工作机会。

架构建议是,您实际上可能需要一个接口的两个实现,而不是两个接口的单一实现:

interface User {}
class MyUser implements User {}
class YourUser implements User {}
interface UserGetter { List<? extends User> getUsers(); }

It is hard to tell what you are asking, but according to the Java Language Specification:

In a situation such as this:

interface Fish { int getNumberOfScales(); }
interface StringBass { double getNumberOfScales(); }
class Bass implements Fish, StringBass {
    // This declaration cannot be correct, no matter what type is used.
    public ??? getNumberOfScales() { return 91; }
}

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 and YourUser have a common ancestor then you could do List<? extends User> or if they have no commonality you can use simply use List<?>.

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...

interface MyUserGetter { List<? extends MyUser> getUsers(); }
interface YourUserGetter { List<? extends YourUser> getUsers(); }
class UserGetterImpl { List<? extends UserImpl> getUsers(); }

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:

interface User {}
class MyUser implements User {}
class YourUser implements User {}
interface UserGetter { List<? extends User> getUsers(); }
是伱的 2024-09-16 02:43:41

简短的回答是:这是不可能的。您不能拥有两个签名仅因返回类型不同而不同的方法,因此不能让一个类实现两个定义仅因返回类型不同的方法的接口。

简单的解决方法是使 MyUsersGetter 和 YourUsersGetter 具有不同名称的方法。

一种可能的解决方法是让 UsersGetterImpl 不直接实现 MyUsersGetter 和 YourUsersGetter,而是使用委托:

class UsersGetterImpl {
  public MyUsersGetter getMyUsers () {
    return new MyUsersGetter () {
      public List<MyUsers> getUsers () {
        //do stuff here
      }
    }
  }

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:

class UsersGetterImpl {
  public MyUsersGetter getMyUsers () {
    return new MyUsersGetter () {
      public List<MyUsers> getUsers () {
        //do stuff here
      }
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文