在下面的代码中,我想将'Races.class'作为参数传递给方法'getAllRaces()'。我该怎么做?
public static void getAllRaces() throws ClientProtocolException, IOException
{
Response myReponse = APIExecutor.getRequest("/races");
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.serializeNulls().setPrettyPrinting().create();
Races races = gson.fromJson(myReponse.getReqResponse(), Races.class);
...
}
答案 0 :(得分:3)
将您的方法更改为:
public static void getAllRaces(Class<?> clazz) {
...
Races races = gson.fromJson(myReponse.getReqResponse(), clazz);
...
}
你可以这样称呼它:
getAllRaces(Races.class)
答案 1 :(得分:0)
Races.class
作为Class<Races>
类型的对象。在您的情况下,您可以使用?
捕获来传递未知类型的类:
public static void getAllRaces(Class<?> raceClass) {
//...
}