本帖最后由 zhangww1985 于 2011-08-05 17:42:04 编辑

解决方案 »

  1.   


    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    public class Test {
    public static class A {

    private int a = 111;

    public void print(){
    System.out.println("In Test.A,a="+a);
    }
    public int getA()
    {
    return a;
    }
    }

    public void print()
    {
    System.out.println("In Test,a="+new A().getA());
    }

    public static void main(String[] args)
    { try {
    Class test = Class.forName("Test");
    Object o = test.newInstance();

    Method[] m=test.getDeclaredMethods();

    for(Method method : m)
    {
    if(method.getName().contains("print"))
    {
    method.invoke(o, null);
    }
    }
    Class testa = Class.forName("Test$A");
    Object oa = testa.newInstance(); Method[] ma=testa.getDeclaredMethods();

    for(Method methoda : ma)
    {
    if(methoda.getName().contains("print"))
    {
    methoda.invoke(oa, null);
    }
    }
    } catch (SecurityException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (InstantiationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (InvocationTargetException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }






    }

    }
      

  2.   

    反射内部类,内部类的命名是包名.Outer$Inner.Class b = Class.forName("A"+"$B");Object B = b.newInstance();
      

  3.   

    因为你的内部类不是static的,所以必须先new A,然后才能new内部类
    即非反射方式的的时候
    B b = new A().new B(); //是通过这样才生成B实例的
    同样道理,反射的时候也必须先new A才能new B