Java generics method overriding

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

I have interface:

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

3条回答
  •  北荒
    北荒 (楼主)
    2021-01-23 00:25

    Use

    public 
        GetCartResponse execute(GetCart action) {
        //do stuff
    }
    

    as the signature for the execute method in CartService, it should work.

    I ahve tested that the follwing compiles; just substituted ActionResponse with Integer, and Action with List, for convenience.

    CartService:

    public interface CartService {
         T execute(List action);
    }
    

    XX:

    public class XX implements CartService {
        public  T execute(List action) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }
    

提交回复
热议问题