java反射调用方法
1、Talk is cheap.Show me the code.code:package chapter3;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;/*** Created by MyWorld on 2016/3/16.*/public class RefactorInvokeMethodDemo { public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException { Class<Business> businessClass = Business.class; Business business = businessClass.newInstance(); Method sayHelloMethod = businessClass.getDeclaredMethod("sayHello", String.class); Object result = sayHelloMethod.invoke(business, "Baidu文库"); System.out.println(result); }}class Business { public String sayHello(String guestName) { return "Hello ," + guestName; }}

2、执行上面的代码,看看会输出的结果是什么?是不是期待的“Hello ,Baidu文库”还是真是!!!应该是嘛,完全是按api来的喽

4、能不调用private的方法呢?是可以的。Show the code.Code:private String sayHello(String guestName) { return "Hello ," + guestName;}

6、哦。还需要设置一个地方,更改方法的private属性Code:Method sayHelloMethod = businessClass.getDeclaredMethod("sayHello", String.class);sayHelloMethod.setAccessible(true);
