1

我希望在 java 中创建一个模仿以下 C# 方法的功能的通用帮助器方法。

private T Execute<T>(Request request) where T : Response, new()
{
     .... Code
}

上面的方法在以下情况下被调用

    public AddLogicalDeviceResponse AddLogicalDevice(AddLogicalDeviceRequest request)
    {
        return Execute<AddLogicalDeviceResponse>(request);
    }


    public GetConfigurationResponse GetConfiguration(GetConfigurationRequest request)
    {
        return Execute<GetConfigurationResponse>(request);
    }


    public GetDeviceDefinitionsResponse GetDeviceDefinitions(GetDeviceDefinitionsRequest request)
    {
        return Execute<GetDeviceDefinitionsResponse>(request);
    }


    public RemoveLogicalDeviceResponse RemoveLogicalDevice(RemoveLogicalDeviceRequest request)
    {
        return Execute<RemoveLogicalDeviceResponse>(request);
    }


    public GetEPCListCommandResponse GetEPCListCommand(GetEPCListCommandRequest request)
    {
        return Execute<GetEPCListCommandResponse>(request);
    }


    public DeviceSpecificCommandResponse DeviceSpecificCommand(DeviceSpecificCommandRequest request)
    {
        return Execute<DeviceSpecificCommandResponse>(request);
    }


    public GetConfigValuesCommandResponse GetConfigValuesCommand(GetConfigValuesCommandRequest request)
    {
        return Execute<GetConfigValuesCommandResponse>(request);
    }


    public SetConfigValuesCommandResponse SetConfigValuesCommand(SetConfigValuesCommandRequest request)
    {
        return Execute<SetConfigValuesCommandResponse>(request);
    }


    public UpdateFirmwareCommandResponse UpdateFirmwareCommand(UpdateFirmwareCommandRequest request)
    {
        return Execute<UpdateFirmwareCommandResponse>(request);
    }

上述每种情况都在各自的情况下扩展了一个类 Response 或 Request。

我尝试使用 Java 泛型创建与上述 ​​C# 代码相同的实现,但没有任何成功。是否可以仅使用一个辅助方法来实现相同的功能,或者我是否被迫为每种单独的调用类型创建一个单独的辅助方法。

编辑 :

这是我当前的 Java 实现

private <T extends Response> execute(Request request)
{
    String url = request.BuildUri(_baseUri, _accessKey);
    Object[] response = Send(url, "");

    if(((Integer)response[1] == 200))
    {
        T jsResponse = new T();
        jsResponse.Body = (String)response[0];
        jsResponse.Request = url;
        return jsResponse;
    }
    else
    {
        throw new ResponseException(response[1], response[2], url, response[0]);
    }
}

我目前的麻烦源于函数无法将返回识别为有效对象。无法将对象 T 初始化为响应,并且 T 对象无法识别响应中实现的方法。

4

2 回答 2

3

在 Java 中,如果不作为参数T发送,就无法创建类的实例。Class<T>这是将其用于当前选项的示例:

interface Request {}

interface Response {
    public String sayHi();
}

class DeviceRequest implements Request {}
class DeviceResponse implements Response{
    public String sayHi() { return "DeviceResponse"; }
}

class WebRequest implements Request {}
class WebResponse implements Response{
    public String sayHi() { return "WebResponse"; }
}

class Helper {
    private <T extends Response> T execute(Request request, Class<T> clazz) throws InstantiationException, IllegalAccessException {
        return clazz.newInstance();
    }
    public DeviceResponse foo(DeviceRequest request) throws InstantiationException, IllegalAccessException {
        return execute(request, DeviceResponse.class);
    }
    public WebResponse bar(WebRequest request) throws InstantiationException, IllegalAccessException {
        return execute(request, WebResponse.class);
    }
}

class Consumer {
    public void baz() throws InstantiationException, IllegalAccessException {
        Helper helper = new Helper();
        DeviceResponse dr = helper.foo(new DeviceRequest());
        WebResponse wr = helper.bar(new WebRequest());
        System.out.println(dr.sayHi());
        System.out.println(wr.sayHi());
    }
    public static void main(String[] args) throws Exception {
        new Consumer().baz();
    }
}

请注意,由于这只是一个示例,我更喜欢只抛出异常而不处理它们。对于现实世界的应用程序,您应该处理异常而不是仅仅抛出它们。

于 2013-05-31T14:46:02.797 回答
1

在 Java 中,您在签名的其他部分之前定义通用参数。

private <T extends Response> T execute(Request request)
{
     .... Code
}
于 2013-05-31T15:00:05.067 回答