Java 编译器如何处理多个泛型边界?
2022-09-04 02:29:44
看看这个(可以说是愚蠢的)代码:
public <T extends Appendable & Closeable> void doStuff(T object)
throws IOException{
object.append("hey there");
object.close();
}
我知道编译器会删除通用信息,所以我对Java 1.4代码感兴趣,这些代码等同于编译器所做的(我很确定编译器不会重新排列源代码,所以我要求一个等效的Java源代码版本,像我这样天真的人可以理解)
Is 是这样的:
public void doStuff(Object object)
throws IOException{
((Appendable)object).append("hey there");
((Closeable)object).close();
}
或者更确切地说,像这样:
public void doStuff(Object object)
throws IOException{
Appendable appendable = (Appendable) object;
Closeable closeable = (Closeable) object;
appendable.append("hey there");
closeable.close();
}
甚至像这样:
public void doStuff(Appendable appendable)
throws IOException{
Closeable closeable = (Closeable) appendable;
appendable.append("hey there");
closeable.close();
}
还是另一个版本?