现在有个接口   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 求高手 解决 

解决方案 »

  1.   

    Class testinterface= Class.froName("com.test.Test");Object objectresult= testinterface.cast(ob);
     
    返回的 还是com.test.Testclass 类型的 对象  并没有转成com.test.Test
      

  2.   

    have a try
    setinfo.invoke(local,(com.test.Test)ob); //把ob强行转换,因为ob是object类型不是Test类型
      

  3.   

    晕...错误一大堆..Method setinfo=classloal.getMethod("setTest",Class.forName("com.test.Test"));
    //中第二个参数是是要你传入setTest的参数进去,不是类。
    //改为:getMethod("setTest",new Test());public interface Test{ pubic int ss;} 
    //接口定义错误,接口的属性必须为常量,方法均为抽象才可以。
    pubic class Testclass implement Test{}
    //public class Testclass implements Test
      

  4.   


    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());
    }
    }没啥大问题,小问题一堆。
      

  5.   


    这个getMethod方法是我用错了...
      

  6.   

    主要是  这有个 问题 ,  我的运行类  和  Test/Testclass/TestLocal  不在同一个项目
      

  7.   


    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());
    }
    }
    楼主要细心点
      

  8.   

    哈哈哈   谢谢大家  哈哈  问题解决了   艾  我的Method太多了   我运行错了  555555555555555555555    我说呢  咋老报 类型错误   郁闷呢  脑袋晕死。。 谢谢大家啊