How to specific a Java Generic class dynamicly

后端 未结 2 1216
青春惊慌失措
青春惊慌失措 2021-01-23 06:11

If I specific a method which return a generic class,how can I do than I can specific the type of generic class dynamicly ? for example

  try {
        Class c =C         


        
2条回答
  •  -上瘾入骨i
    2021-01-23 06:51

    Generic syntax is good at compile-time only. None of the types in a generic class or method are available at runtime. This is called type erasure. So you cannot do what you want to do, at least not this way.

    Depending on the original problem you are trying to solve, you may be able to use wildcards instead.

提交回复
热议问题