简体   繁体   English

使用继承的泛型类型实例化

[英]instantiate using an inherited generic type

I have the following case 我有以下情况

public class ResponseType <T> {
  private ParameterizedTypeReference<HttpResponse<T>> type;

  private ResponseType() {
    this.type = new ParameterizedTypeReference<HttpResponse<T>>() {};
  }

  public static <T> ResponseType<T> create() {
    return new ResponseType<T>();
  }
}

When I call ReponseType.<MyClass>create() where how can I construct a ParameterizedTypeReference<HttpResponse<MyClass>> ? 当我调用ReponseType.<MyClass>create() ,如何构造ParameterizedTypeReference<HttpResponse<MyClass>>

Currently if I call this, the type will be ParameterizedTypeReference<HttpResponse<T>> 当前,如果我将其称为,则类型将为ParameterizedTypeReference<HttpResponse<T>>

What I want to do is to call 我想做的就是打电话

restTemplate.exchange(
    ...,
    responseType.getType()
);

where responseType = ResponseType.create() or resonseType = ResponseType.<MyClass>create() 其中responseType = ResponseType.create()resonseType = ResponseType.<MyClass>create()

instead of 代替

restTemplate.exchange(
    ...,
    new ParameterizedTypeReference<HttpResponse<MyClass>>() {}
);

You are creating in the constructor that is called by create() witht he type T and storing inside the class as 'type' 您正在创建一个由create()调用的构造函数,其类型为T并在类内部存储为“ type”

If its public: 如果是公众:

ParameterizedTypeReference<HttpResponse<MyClass>> obj = ReponseType.<MyClass>create().type;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM