Get the interface name from the implementation class

后端 未结 2 1693
醉梦人生
醉梦人生 2020-12-20 15:44

Example :

List list = new ArrayList();
//This would give me the class name for the list reference variable.
list.getClass().getSi         


        
相关标签:
2条回答
  • 2020-12-20 16:17
    Class  aClass = ... //obtain Class object. 
    Class[] interfaces = aClass.getInterfaces();
    
    0 讨论(0)
  • 2020-12-20 16:21

    Using Reflection you can invoke the Class.getInterfaces() method which returns an Array of Interfaces that your class implements.

    list.getClass().getInterfaces()[0];
    

    To get just the name

    list.getClass().getInterfaces()[0].getSimpleName();
    
    0 讨论(0)
提交回复
热议问题