开发者

problem with wildcards and one class which implements two interfaces

I have this question.

I have 开发者_JAVA技巧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...)


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(); }


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
      }
    }
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜