反射,获得字符串"ABC"后调用A("BC"),在A中根据参数"BC"调用B("C"),在B中根据参数"C"调用C(""),就ok了

解决方案 »

  1.   


    package test;import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;public class Test {
    @SuppressWarnings("unused")
    public static void main(String[] args) {
    schedule("abc");
    }
    static void schedule(String methods) {
    Test t=new Test();
    if (methods.length()==0) {
    return;
    }
    String m=methods.substring(0,1);
    String p=methods.substring(1);
    try {
    Method method=Test.class.getDeclaredMethod(m,String.class);
    method.invoke(t,p);
    } catch (NoSuchMethodException e) {
    e.printStackTrace();
    } catch (SecurityException e) {
    e.printStackTrace();
    } catch (IllegalAccessException e) {
    e.printStackTrace();
    } catch (IllegalArgumentException e) {
    e.printStackTrace();
    } catch (InvocationTargetException e) {
    e.printStackTrace();
    }
    }
    void a(String methods) {
    System.out.println("Test.a()");
    schedule(methods);
    }
    void b(String methods) {
    System.out.println("Test.b()");
    schedule(methods);
    }
    void c(String methods) {
    System.out.println("Test.c()");
    schedule(methods);
    }
    void d(String methods) {
    System.out.println("Test.d()");
    schedule(methods);
    }
    }