类A的路径是reflect.A
class A
{
  interface b
{
}
 class B implents b
{
}
}
如何通过反射获取b和B??

解决方案 »

  1.   

    Class[] clzs = A.class.getDeclaredClasses();
    可以得到所有A类中定义的内嵌类和接口,然后自己遍历吧API说明:
    Returns an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object. This includes public, protected, default (package) access, and private classes and interfaces declared by the class, but excludes inherited classes and interfaces. This method returns an array of length 0 if the class declares no classes or interfaces as members, or if this Class object represents a primitive type, an array class, or void.
      

  2.   

    import java.lang.reflect.Modifier;public class Test {
    @SuppressWarnings("rawtypes")
    public static void main(String[] args) throws Exception {
    Class thisclass = Test.class;
    Class classes[] = thisclass.getDeclaredClasses();   
            for (Class c:classes){
                int  i = c.getModifiers();   
                String s = Modifier.toString(i);
                if(s.contains("interface")){
                 System.out.println("接口:" + c.getName());
                }else{
                 System.out.println("内部类:" + c.getName());
                }
            }   
    } interface IB {
    } class B implements IB {
    }
    }
      

  3.   

    楼主可以查下,getDeclaredClasses和getClasses的区别