两个一模一样的方法,就是有一个参数不同,能不能用一个方法来代替这两个方法? 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, )
}
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, )
}
2中的arglist也是一个数组,只有一个元素,是一个ActiveRecord 执行结果有区别吗感觉可以合并,因为invoke方法传入的是一个当前类的句柄和一个Object
这个方法的参数是不确定的,有可能是数组有可能是单一的对象,
所以比较难办
感觉ActiveRecord在函数中根本没用到。可以合并吧
当你需要用到第二个方法时,做个数组,把那个record放到数组的第一个元素里
这样不就可以了吗。
上面我说了这个meth.invoke方法的参数是不确定的,有可能是数组对象有可能是普通的对象,
所以传参数之前我必须要有一个判断,
如何判断一个object是数组对象还是普通的对象呢?
System.out.println(ss.getClass());
Object[] s = {"1","2"};
System.out.println(s.getClass());打印结果class java.lang.String
class [Ljava.lang.Object;
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, )
}
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, )
}
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});
}