簡體   English   中英

使用繼承的泛型類型實例化

[英]instantiate using an inherited generic type

我有以下情況

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>();
  }
}

當我調用ReponseType.<MyClass>create() ,如何構造ParameterizedTypeReference<HttpResponse<MyClass>>

當前,如果我將其稱為,則類型將為ParameterizedTypeReference<HttpResponse<T>>

我想做的就是打電話

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

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

代替

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

您正在創建一個由create()調用的構造函數,其類型為T並在類內部存儲為“ type”

如果是公眾:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM