How to access the .class from a class with a Generic?

后端 未结 2 1431
Happy的楠姐
Happy的楠姐 2020-12-06 17:55

When it comes to classes without generics, i can access this .class attribute like this:

class Foo{
    Class getMyClass(){
        return Foo.cla         


        
2条回答
  •  误落风尘
    2020-12-06 19:00

    There isn't a way, because of type erasure. This:

    Foo.class
    

    ... can not be obtained at runtime, it will always be the same type no matter what's the type of T. At runtime only this exists:

    Foo.class
    

提交回复
热议问题