How to do `MyClass.class` in Java?

前端 未结 6 1276
有刺的猬
有刺的猬 2020-12-15 21:46

How can call public T doit(Class clazz); using MyClass.class as clazz where I can not instantiate or extend MyClas

6条回答
  •  轮回少年
    2020-12-15 22:08

    How to do MyClass.class in Java?

    You can't.

    Generics in Java use type erasure; the type of the parametrized argument is enforced during compilation, but it is lost after compilation. The resulting byte code for an instance of a generic class does not contain any run-time meta-data on its arguments whatsoever.

    As it is now, it is just not possible, a major language design blunder IMO.

提交回复
热议问题