如何将泛型传递给需要超类的方法?

时间:2017-02-16 15:41:12

标签: java

我有一些课

public final class Response<T> {

}

我有超类

public class BaseResponse {

}

从超类我继承

public class LoginResponse extends BaseResponse{

}

稍后我创建了对象Response<LoginResponse> myResponse,我想将此对象传递给以下方法,该方法仅适用于超类方法和字段:

public static boolean responseIsValid(Response<BaseResponse> response, Context context) {
     return false;
}

我收到wrong argument type错误。如何正确传递?

1 个答案:

答案 0 :(得分:3)

您可以将$ touch a1 a2 a3 $ echo a* # <-- expansion, = 'echo a1 a2 a3' a1 a2 a3 $ echo "a*" # <-- no expansion, = 'echo a*' a* 替换为通配符Response<BaseResponse>

Response<? extends BaseResponse>

当然public static boolean responseIsValid(Response<? extends BaseResponse> response, Context context) { return false; } 确实延伸LoginResponse

BaseResponse