Java 与 base 和 child 相关的通用 class "not applicable for the arguments"
Java Generic relating to base and child class "not applicable for the arguments"
我确实发现了关于一般问题的类似帖子,但没有一个以我能理解的方式解决了我的问题。我希望有人能帮我解决这个问题。
我尝试了很多方法,比如从不同的地方删除“?extends”并尝试使用“?super”来查看另一个错误是否会为我指明解决方案的方向。
以下代码:
final ResponseBase response = executor.execute(myRequest);
给我以下编译器错误:
"The method execute(capture#6-of ? extends RequestBase) in the type CommandExecutor is not applicable for the arguments (RequestBase)"
完整代码清单:
public class MainClass {
private final static Map<Class<? extends RequestBase>, CommandExecutor<? extends RequestBase, ? extends ResponseBase>> MAP = new HashMap<>();
public static void main(String[] args) {
final DummyCommandExecutor dummyCommandExecutor = new DummyCommandExecutor();
MAP.put(MyRequest.class, dummyCommandExecutor);
final RequestBase myRequest = new MyRequest();
myRequest.setRequestString("this is my request");
final CommandExecutor<? extends RequestBase, ? extends ResponseBase> executor = MAP.get(myRequest.getClass());
final ResponseBase response = executor.execute(myRequest);
System.out.println(response.getResponseString());
}
}
interface CommandExecutor<T, R> {
R execute(T object);
}
class DummyCommandExecutor implements CommandExecutor<MyRequest, MyResponse> {
@Override
public MyResponse execute(MyRequest request) {
final MyResponse response = new MyResponse();
response.setResponseString(request.getRequestString());
return response;
}
}
class MyResponse extends ResponseBase {
}
class ResponseBase {
String responseString;
public String getResponseString() {
return this.responseString;
}
public void setResponseString(String responseString) {
this.responseString = responseString;
}
}
class MyRequest extends RequestBase {
}
class RequestBase {
String requestString;
public String getRequestString() {
return this.requestString;
}
public void setRequestString(String requestString) {
this.requestString = requestString;
}
}
不进行转换就无法执行此操作。虽然您知道映射将 return 是给定类型的正确命令执行程序,但编译器不会,因此您需要告诉编译器不要关心:
final CommandExecutor<RequestBase,ResponseBase> executor = (CommandExecutor) MAP.get(myRequest.getClass());
你不能只匹配吗?与 T
一个选项就是做:
final CommandExecutor executor = MAP.get(myRequest.getClass());
如果地图 returns smth 不可施法
,当然可以在哪个选项中获得 运行 时间例外
我确实发现了关于一般问题的类似帖子,但没有一个以我能理解的方式解决了我的问题。我希望有人能帮我解决这个问题。
我尝试了很多方法,比如从不同的地方删除“?extends”并尝试使用“?super”来查看另一个错误是否会为我指明解决方案的方向。
以下代码:
final ResponseBase response = executor.execute(myRequest);
给我以下编译器错误: "The method execute(capture#6-of ? extends RequestBase) in the type CommandExecutor is not applicable for the arguments (RequestBase)"
完整代码清单:
public class MainClass {
private final static Map<Class<? extends RequestBase>, CommandExecutor<? extends RequestBase, ? extends ResponseBase>> MAP = new HashMap<>();
public static void main(String[] args) {
final DummyCommandExecutor dummyCommandExecutor = new DummyCommandExecutor();
MAP.put(MyRequest.class, dummyCommandExecutor);
final RequestBase myRequest = new MyRequest();
myRequest.setRequestString("this is my request");
final CommandExecutor<? extends RequestBase, ? extends ResponseBase> executor = MAP.get(myRequest.getClass());
final ResponseBase response = executor.execute(myRequest);
System.out.println(response.getResponseString());
}
}
interface CommandExecutor<T, R> {
R execute(T object);
}
class DummyCommandExecutor implements CommandExecutor<MyRequest, MyResponse> {
@Override
public MyResponse execute(MyRequest request) {
final MyResponse response = new MyResponse();
response.setResponseString(request.getRequestString());
return response;
}
}
class MyResponse extends ResponseBase {
}
class ResponseBase {
String responseString;
public String getResponseString() {
return this.responseString;
}
public void setResponseString(String responseString) {
this.responseString = responseString;
}
}
class MyRequest extends RequestBase {
}
class RequestBase {
String requestString;
public String getRequestString() {
return this.requestString;
}
public void setRequestString(String requestString) {
this.requestString = requestString;
}
}
不进行转换就无法执行此操作。虽然您知道映射将 return 是给定类型的正确命令执行程序,但编译器不会,因此您需要告诉编译器不要关心:
final CommandExecutor<RequestBase,ResponseBase> executor = (CommandExecutor) MAP.get(myRequest.getClass());
你不能只匹配吗?与 T
一个选项就是做:
final CommandExecutor executor = MAP.get(myRequest.getClass());
如果地图 returns smth 不可施法
,当然可以在哪个选项中获得 运行 时间例外