日期:2014-05-20 浏览次数:21086 次
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; interface Test { } class Testclass implements Test { } public class Testlocal { public void setTest(Test test) { System.out.print("Hello World!"); } public static void testRun(Object ob) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException { String classname = "Testlocal"; Class classlocal = Class.forName(classname); Object local = classlocal.newInstance(); Method setinfo = classlocal.getMethod("setTest", Class.forName("Test")); setinfo.invoke(local, ob); } public static void main(String args[]) throws SecurityException, IllegalArgumentException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { testRun(new Testclass()); } }
------解决方案--------------------
interface Test { public int ss = 1;; } class Testclass implements Test { } public class Testlocal { public void setTest(Test test) { System.out.println("ABC"); } public static void testrun(Object ob) throws Exception { String classname = "testNew.Testlocal"; Class classlocal = Class.forName(classname); Object local = classlocal.newInstance(); Method setinfo = classlocal.getMethod("setTest", Class.forName("testNew.Test")); setinfo.invoke(local, ob); } public static void main(String[] args) throws Exception{ testrun(new Testclass()); } }