为什么以下不起作用?
void execute() {
Integer a = Integer.valueOf(1);
a = reassign(a);
D.log("a: " + a);
}
<T extends Integer> T reassign(T t) {
t = Integer.valueOf(2); // error: incompatible types: Integer cannot be converted to T
// t = (T) Integer.valueOf(2); // This works but with warning: [unchecked] unchecked cast
return t;
}
<T extends Integer> T reassign2(T t, T anotherT) {
t = anotherT; // This works without any warning.
return t;
}
我的理解是,通用方法/类/接口将被编译为单个类文件,其中类型参数被替换为最合适的下限(在上述情况下为整数)。
Java 环境:java 11.0.4 2019-07-16 LTS
慕虎7371278
胡说叔叔
相关分类