Java: T obj; type of obj.getClass() is Class<?> and not Class<? extends T>. why?

后端 未结 4 563
春和景丽
春和景丽 2020-12-31 12:42

In such a function:

 void foo(T obj)

The type of obj.getClass() is Class and not Class<

4条回答
  •  囚心锁ツ
    2020-12-31 13:14

    The basic problem is that getClass() doesn't return the class because its defined at the Object level. i.e. it is mearly defined as a class which extends object. They could have defined getClass() like.

    Class getClass() { /**/ }
    

    but instead its

    Class getClass()
    

    which means generics has no understanding of what getClass returns.

提交回复
热议问题