我試着明白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