现在有个接口 public interface Test{ pubic int ss;}
一个类继承这个接口 pubic class Testclass implement Test{}
有一个类 public class Testlocal{
public void setTest(Test test){ }
} public static testrun(Obect ob){
String classname="com.test.Testlocal";
Class classlocal=Class.forName(classname);
Object local=classlocal.newInstance();
Method setinfo=classloal.getMethod("setTest",Class.forName("com.test.Test"));
setinfo.invoke(local,ob);
}
但是 ob 是com.test.Testclass 类型的 直接运行 报 argument type match 求高手 解决
一个类继承这个接口 pubic class Testclass implement Test{}
有一个类 public class Testlocal{
public void setTest(Test test){ }
} public static testrun(Obect ob){
String classname="com.test.Testlocal";
Class classlocal=Class.forName(classname);
Object local=classlocal.newInstance();
Method setinfo=classloal.getMethod("setTest",Class.forName("com.test.Test"));
setinfo.invoke(local,ob);
}
但是 ob 是com.test.Testclass 类型的 直接运行 报 argument type match 求高手 解决
解决方案 »
- 求运行JAVA语言的最佳方法??
- 使用java批量删除文件
- java mail 使用pop3的协议怎么判断新邮件
- AWT与SWING的区别
- 滚屏后,我的JLabel为什么不能向下拉了??
- 用DataExpress组件连接数据库时,如何编写鼠标双击单个纪录进行修改的鼠标事件
- www.google.com为什么换成了百度????谁知道是为什么?请斑竹别删
- 请大家帮我看段程序
- 一个程序放到jbpersonal中和放到jbenprise中不一样么?为什么在jbP中编译执行成功,而在jbe中找不到类?
- 是初始化问题吗,急,请帮忙解决?
- 300万件数据测试时,使用setFetchSize方法疑惑
- 关于Jtable行选中事件的疑问
返回的 还是com.test.Testclass 类型的 对象 并没有转成com.test.Test
setinfo.invoke(local,(com.test.Test)ob); //把ob强行转换,因为ob是object类型不是Test类型
//中第二个参数是是要你传入setTest的参数进去,不是类。
//改为:getMethod("setTest",new Test());public interface Test{ pubic int ss;}
//接口定义错误,接口的属性必须为常量,方法均为抽象才可以。
pubic class Testclass implement Test{}
//public class Testclass implements Test
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());
}
}没啥大问题,小问题一堆。
这个getMethod方法是我用错了...
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());
}
}
楼主要细心点