简体   繁体   English

类型擦除会干扰多态性

[英]Type erasure interferes with polymorphism

I was reading Core Java Volume I and I encounter a question in translating generic expression. 我正在阅读Core Java Volume I,在翻译通用表达式时遇到一个问题。 Here is what the book said. 这是书上所说的。

在此处输入图片说明

I don't understand why there is a interfere. 我不明白为什么会有干扰。 For the highlighted sentence, why setSecond(Object) method is called? 对于突出显示的句子,为什么调用setSecond(Object)方法? Shouldn't it be setSecond(Date) method since interval is a DateInterval object? 因为interval是一个DateInterval对象,所以不应该使用setSecond(Date)方法吗?

The main problem, I think, is that all that comes between brackets (in this case ) only exists when you're writing the code. 我认为主要的问题是方括号之间的所有内容(在本例中为)仅在编写代码时存在。 It's a compilation-time checking. 这是编译时检查。 That's what the type erasure is all about. 这就是类型擦除的全部内容。 When you compile the code, all you have are references to Object and these bridge methods in the bytecodes. 编译代码时,您所拥有的只是在字节码中对Object和这些桥接方法的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM