Java generics method overriding

后端 未结 3 1218
天涯浪人
天涯浪人 2021-01-22 23:33

I have interface:

public interface CartService extends RemoteService{
     T execute(Action action);
}

3条回答
  •  借酒劲吻你
    2021-01-23 00:16

    Your interface specifies that implementing classes must define that method for all T extends ActionResponse. You want to define them for specific actions, and responses - I think your interface needs to be

        public interface CartService>
         extends RemoteService {
          public T execute(A action)
        }
    

    and then implementation would be

    public class XX implements CartService {
     //...
    }
    

    As written, you may not need to explicitly parametrize with an extension of Action type - as long as you can deal with any kind of Action that is parametrized by GetCartResponse and don't rely on specifics of the GetCart action. In which case, your interface should look something like:

    public interface CartService extends RemoteService {
     public T execute(Action action);
    }
    

    and implementation

    public class XX implements CartService {
     public GetCartResponse execute(Action action) {
      //...
     }
    }
    

提交回复
热议问题