Overriding method with generics not working (no method found)

左心房为你撑大大i 提交于 2019-12-05 07:53:21

The method onPostAction in Player is itself generic. You have defined P there. Therefore, any method that overrides it must also be generic.

Try

public class RPSHumanPlayer extends RPSPlayer {
    @Override
    public <P extends Player<RPSGesture, RPSResult>> void onPostAction(final P target,
       final RPSGesture gesture, final RPSResult result) { }
}

A and R are already defined by GesturePlayer to be RPSGesture and RPSResult, but P still needs to be declared.

Addition

If it needs to be that exact signature, then you'll have to define P along with A and R on the Player interface:

public interface Player<A extends Action, R extends Result, P extends Player<A, R, P>> {
    default public void onPostAction(final P target, final A action, final R result) { }
}

Then GesturePlayer changes accordingly:

abstract public class GesturePlayer<A extends Action, R extends Result,
     P extends Player<A, R, P>> implements Player<A, R, P> { }

Then RPSPlayer defines itself to be P.

abstract public class RPSPlayer extends GesturePlayer<RPSGesture, RPSResult, RPSPlayer> { }

And RPSHumanPlayer can have the method as is:

public class RPSHumanPlayer extends RPSPlayer {
    @Override
    public void onPostAction(final RPSPlayer target, final RPSGesture gesture, final RPSResult result) { }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!