我正在阅读一个教程,我发现了这个特定的代码。
private <V> V fromJson(HttpRequest request, Class<V> target) throws IOException {
Reader reader = request.bufferedReader();
try {
return GSON.fromJson(reader, target);
} catch (JsonParseException e) {
throw new JsonException(e);
} finally {
try {
reader.close();
} catch (IOException ignored) {
// Ignored
}
}
}
我注意到 fromJson 函数有两种返回类型?我有泛型的基本概念以及它是如何工作的。我无法理解的是如何指定两种类型,以及该函数如何知道在调用时将值分配给哪种类型。