两个一模一样的方法,就是有一个参数不同,能不能用一个方法来代替这两个方法?    public void setRelationRecord( String methodName, String tableName, ActiveRecord[] record ) throws Exception {
        Class partypes[] = new Class[1];
        Method meth = null;
        partypes[0] = String.class;
        meth = this.getClass().getMethod( methodName, partypes );
        Object arglist[] = new Object[1];
        arglist[0] = record;
        meth.invoke( this, arglist );     
        setRelationRecord( methodName, tableName,  )
    }    public void setRelationRecord( String methodName, String tableName, ActiveRecord record ) throws Exception {
        Class partypes[] = new Class[1];
        Method meth = null;
        partypes[0] = String.class;
        meth = this.getClass().getMethod( methodName, partypes );
        Object arglist[] = new Object[1];
        arglist[0] = record;
        meth.invoke( this, arglist );     
        setRelationRecord( methodName, tableName,  )
    }

解决方案 »

  1.   

    还是有区别1中的arglist是一个数组,只有一个元素是ActiveRecord数组类型
    2中的arglist也是一个数组,只有一个元素,是一个ActiveRecord 执行结果有区别吗感觉可以合并,因为invoke方法传入的是一个当前类的句柄和一个Object
      

  2.   

    这个meth.invoke( this, arglist );   实际要执行的方法是一个 setXxxx()方法,
    这个方法的参数是不确定的,有可能是数组有可能是单一的对象,
    所以比较难办
      

  3.   

    不知道你对反射具体有什么要求。
    感觉ActiveRecord在函数中根本没用到。可以合并吧
      

  4.   

    如何判断长度1的数组是个普通object ?
      

  5.   

    to navence(卡西C) : ActiveRecord是meth.invoke( this, arglist ); 这里调用方法的一个参数
      

  6.   

    同意 whyxx(不知道该取个什么名字) 的看法
      

  7.   

    如何判断长度1的数组是个普通object ?
      

  8.   

    直接就用第一个数组那个方法就行
    当你需要用到第二个方法时,做个数组,把那个record放到数组的第一个元素里
    这样不就可以了吗。
      

  9.   

    因为直接合成一个方法的话就需要判断传进来的是数组对象还是普通对象了,
    上面我说了这个meth.invoke方法的参数是不确定的,有可能是数组对象有可能是普通的对象,
    所以传参数之前我必须要有一个判断,
    如何判断一个object是数组对象还是普通的对象呢?
      

  10.   

    Object ss = "1";
            System.out.println(ss.getClass());
            Object[] s = {"1","2"}; 
            System.out.println(s.getClass());打印结果class java.lang.String
    class [Ljava.lang.Object;
      

  11.   

    ss  和   s 可以通过一个参数传入到方法中吗?
      

  12.   

    不论s还是ss都是Object对象,因为数组本身也是Object
      

  13.   

    public void setRelationRecord( String methodName, String tableName, ActiveRecord[] record ) throws Exception {
    Class partypes[] = new Class[1];
    Method meth = null;
    partypes[0] = String.class;
    meth = this.getClass().getMethod( methodName, partypes );
    Object arglist[] = new Object[1];
    arglist[0] = record;
    meth.invoke( this, arglist );
    setRelationRecord( methodName, tableName, )
    }public void setRelationRecord( String methodName, String tableName, Object record ) throws Exception {
    Class partypes[] = new Class[1];
    Method meth = null;
    partypes[0] = String.class;
    meth = this.getClass().getMethod( methodName, partypes );
    Object arglist[] = new Object[1];
    arglist[0] = record;
    meth.invoke( this, arglist );
    setRelationRecord( methodName, tableName, )
    }
      

  14.   

    不好意思,前一个删掉
    public void setRelationRecord( String methodName, String tableName, Object record ) throws Exception {
    Class partypes[] = new Class[1];
    Method meth = null;
    partypes[0] = String.class;
    meth = this.getClass().getMethod( methodName, partypes );
    Object arglist[] = new Object[1];
    arglist[0] = record;
    meth.invoke( this, arglist );
    setRelationRecord( methodName, tableName, )
    }
      

  15.   

    meth.invoke( this, arglist );arglist 里面要求的参数是ActiveRecord类型的,没有强制类型转换会出错误吧
      

  16.   

    这样行不行,不过还是2个方法
    public void setRelationRecord( String methodName, String tableName, ActiveRecord[] record ) throws Exception {
    Class partypes[] = new Class[1];
    Method meth = null;
    partypes[0] = String.class;
    meth = this.getClass().getMethod( methodName, partypes );
    Object arglist[] = new Object[1];
    arglist[0] = record;
    meth.invoke( this, arglist );
    setRelationRecord( methodName, tableName, )
    }public void setRelationRecord( String methodName, String tableName, ActiveRecord record ) throws Exception {
      this.setRelationRecord(methodName,tableName,new ActionRecord[]{record});
    }
      

  17.   

    但是其中的arglist里面的obj有可能是数组有可能是实例,这个不太好办