我试着明白getClass
方法返回的原因是什么Class<? extends |X|>
?为什么getClass返回一个Class <?扩展| X |>?
从openjdk邻近public final native Class<?> getClass();
:
实际结果类型是
Class<? extends |X|>
其中|X|
是静态类型的 表达在其上getClass
被称为的擦除。
为什么不能getClass
有相同的类型,如XClass.class
,例如:
class Foo {}
Foo fooInstance = new Foo();
Class<Foo> fc = Foo.class; // Works!
Class<Foo> fc2 = fooInstance.getClass(); // Type mismatch ;(
Class<?> fc3 = fooInstance.getClass(); // Works!
Class<? extends Foo> fc4 = fooInstance.getClass(); // Works!
相关:HTTP:// stackoverflow.com/questions/19332856/what-is-meant-by-the-erasure-of-the-static-type-of-the-expression-on-which-it-i和http://stackoverflow.com/问题/ 18144556/java-getclass-bound-type – Tunaki