本帖最后由 马林康 于 2012-7-27 22:05 编辑
- public class TestClass {
- public void method_1()
- {
- System.out.println("hello");
- }
- public String method_2(String str){
- return str;
- }
- }
复制代码 反射获取method_1的方法并调用- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- public class ReflectDemo {
- public static void main(String[] args) throws NoSuchMethodException, SecurityException, ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException{
- Method method = Class.forName("io.TestClass").getMethod("method_1");
- method.invoke(Class.forName("io.TestClass").newInstance());
- }
- }
复制代码 代码那里错了 抛出异常
ok解决了 ,上面的代码是正确的了~
|
|