gpt4 book ai didi

带有通配符的 Java 泛型无法编译

转载 作者:搜寻专家 更新时间:2023-11-01 02:38:04 24 4
gpt4 key购买 nike

我做了这个我真实代码的简化示例,它没有编译:

public class Example {

public static void main(String[] args) {
Callback<BalanceResponse> balanceRequestCaller = new Callback<BalanceResponse>() {
@Override
public void onResponse(BalanceResponse response) {}

@Override
public void onFailure(String error, int code) {}

@Override
public void onFailure(Throwable t) {}
};

BalanceRequest breq = new BalanceRequest();

Interceptor interceptor = new Interceptor(balanceRequestCaller);

breq.startRequest((Callback<BaseResponse>) interceptor); //compile-time error!!!
}


public static class Interceptor implements Callback<BaseResponse> {

public Interceptor(Callback<? extends BaseResponse> originalCaller) {
this.originalCaller = originalCaller;
}

private Callback<? extends BaseResponse> originalCaller;

@Override
public void onResponse(BaseResponse response) {
//do some interception stuff

if (response instanceof BalanceResponse) {
((Callback<BalanceResponse>) originalCaller).onResponse((BalanceResponse) response);
} else if (response instanceof SubscriptionResponse) {
((Callback<SubscriptionResponse>) originalCaller).onResponse((SubscriptionResponse) response);
}
}

@Override
public void onFailure(String error, int code) {}

@Override
public void onFailure(Throwable t) {}
}

public interface Request<T extends BaseResponse> {
void startRequest(Callback<T> callback);
}

public static class BalanceRequest implements Request<BalanceResponse> {
@Override
public void startRequest(Callback<BalanceResponse> callback) {}
}

public static class SubscriptionRequest implements Request<SubscriptionResponse> {
@Override
public void startRequest(Callback<SubscriptionResponse> callback) {}
}

public static class BaseResponse {
public String status;
}

public static class BalanceResponse extends BaseResponse {

}

public static class SubscriptionResponse extends BaseResponse {

}

public interface Callback<T> {
void onResponse(T response);
void onFailure(String error, int code);
void onFailure(Throwable t);
}
}

我想做的是制作一个 Interceptor这将拦截回调,对响应做一些事情,然后将其传递给原始回调。

我想做一个通用的 Interceptor可以拦截任何回调 Callback<? extends BaseResponse> .

顺便说一句,我从 NetBeans 得到的编译时错误是

incompatible types: Callback<BaseResponse> cannot be converted to Callback<BalanceResponse>

请给我建议,我怎样才能使这项工作成功?

最佳答案

您应该在类 Interceptor 上定义一个类型参数,以防止编译错误并避免在方法 onResponse 中进行显式转换通常是设计问题的证明。

带有类型参数的拦截器类:

public static class Interceptor<T extends BaseResponse> implements Callback<T> {

public Interceptor(Callback<T> originalCaller) {
this.originalCaller = originalCaller;
}

private Callback<T> originalCaller;

@Override
public void onResponse(T response) {
originalCaller.onResponse(response);
}

...
}

然后您将实例化它并将其用作下一个:

Interceptor<BalanceResponse> interceptor = new Interceptor<>(balanceRequestCaller);
breq.startRequest(interceptor);

关于带有通配符的 Java 泛型无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41714814/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com