find all classes and interfaces a class extends or implements recursively

后端 未结 8 1207
孤独总比滥情好
孤独总比滥情好 2021-02-12 22:13

I was wondering if there was an easy way of determining the complete list of Types that a Java class extends or implements recursively?

for instance:

cla         


        
8条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-12 22:21

    If I got your question right, you want to find all the superclasses (class and interface) of a specific class. If so you can check the following solution

    to find the superclasses

            Class C = getClass();
            while (C != null) {
              System.out.println(C.getSimpleName());
              C = C.getSuperclass();
            }
    

    to find the interfaces

            C = getClass();
            for(Class adf: C.getInterfaces()){
    
                  System.out.println(adf.getSimpleName());
            }
    

提交回复
热议问题