Overriding method with generics not working (no method found)

人盡茶涼 提交于 2019-12-10 04:33:32

问题


I am trying to @Override a method in a class that might look like having a complex inheritance structure, but it should actually be quite simple, however I cannot get it to work.

public interface Action { }

public interface Result { }

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

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

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

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

I get the error on the @Override of the onPostAction, why can it not find the correct method to override?

This is for a Rock-Paper-Scissors implementation for people wondering where the name comes from.

The exact error message:

method does not override or implement a method from a supertype

My goal is to still use the current class, so for RPSHumanPlayer I would actually want to have this signature:

public void onPostAction(final RPSHumanPlayer target, final RPSGesture gesture, final RPSResult result) { }

回答1:


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) { }
}


来源:https://stackoverflow.com/questions/22517047/overriding-method-with-generics-not-working-no-method-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!