通过反射调用类的方法
class B{
public void print(){
System.out.println("B");
}
public void print(int a, int b){
System.out.println("a + b = " + (a + b));
}
}
public static void main(String[] args){
B b = new B();
Class c = b.getClass();
try {
Method m = c.getMethod("print", int.class, int.class);
Object obj = m.invoke(b, 10, 20);
} catch (NoSuchMethodException | SecurityException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}