【发布时间】:2020-09-24 03:26:25
【问题描述】:
从一个例子开始,
public static <T> T method(T str){
return (T)str;
}
// T is deduced to be a String
// This fails at compile time
Integer integer = method("Trial");
//Object obj = method("Trial"); // Old example
和,
public static <T> T method(String str){
return (T)str;
}
// What type does T gets deduced to in this case?
// This compiles but gives an error at run-time.
Integer integer = method("Trial");
//Object obj = method("Trial"); // Old example
两个代码 sn-ps 编译良好。第二个例子中的 T 推导到了哪一种类型?
【问题讨论】:
-
问题是?
-
阅读编译器消息,它们会告诉你问题的真相。
-
有没有办法可以看到实例化的模板?
标签: java