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

后端 未结 2 1432
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 18:44

    You can always do this:

    class Foo{
        Class> getMyClass(){
            return (Class>)(Class)Foo.class
        }
    }
    

    You will have unchecked cast warnings, because it indeed is unsafe -- as others have mentioned, the returned class object is not any more "Foo's class" as "Foo's class".

提交回复
热议问题