所以我有一堆方法,它们本质上是相同的调用,唯一的区别是泛型的类名。例子:
当前的:
public void methodA(ARequest request, ADelegate delegate)
{
JsonClient<ARequest, AResponse> client = new JsonClient<ARequest, AResponse>(request.ServiceServerUrl, request, new AResponse());
client.sendRequest(delegate);
}
public void methodB(BRequest request, BDelegate delegate)
{
JsonClient<BRequest, BResponse> client = new JsonClient<BRequest, BResponse>(request.ServiceServerUrl, request, new BResponse());
client.sendRequest(delegate);
}
我想做的是:
private void serviceCall<R extends RequestBase, S extends ResponseBase>(ADelegate delegate)
{
JsonClient<R, S> client = new JsonClient<R, S>(request.ServiceServerUrl, request, new AResponse());
client.sendRequest(delegate);
}
public void methodA(ARequest request, ADelegate delegate)
{
serviceCall<ARequest, AResponse>(delegate);
}
public void methodB(BRequest request, BDelegate delegate)
{
serviceCall<BRequest, BResponse>(delegate);
}
我认为这在 C# 中是可能的,但我只想知道如何在 Java 中正确地做到这一点。
编辑:为清楚起见。