为什么第一行有效但第二行无效:
Class<? extends Class> c1 = (new Object()).getClass().getClass();
Class<? extends Class<? extends Class>> c2 = (new Object()).getClass().getClass().getClass();
最佳答案
来自 Object.getClass
的 API 文档
The actual result type is Class where |X| is the erasure of the static type of the expression on which getClass is called.
所以(new Object()).getClass()
将返回 Class<? extends Object>
。我们要调用getClass
关于这一点。所以X将是Class
,这将为我们提供 Class<? extends Class>
.
关于java - 如何堆叠 getClass(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13429141/