快速了解反射(Reflection)
import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;public class TestReflection {public static void main(String[] args) throws ClassNotFoundException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException {String str = "T";Class c = Class.forName(str);Object o = c.newInstance();Method[] methods = c.getMethods();for(Method m : methods){//System.out.println(m.getName());if(m.getName().equals("mm")){m.invoke(o);}if(m.getName().equals("m1")){m.invoke(o, 1, 2);Class[] parameters = m.getParameterTypes();for(Class p : parameters){System.out.println(p);}}if(m.getName().equals("getS")){Class returnType = m.getReturnType();System.out.println(returnType);}}}}class T{static{System.out.println("T loaded!");}public void mm(){System.out.println("mm invoked!");}int i;String s;public void m1(int i, int j){this.i = i + j;System.out.println(this.i);}public String getS(){return s;}}