In short, I have a function that accepts a variable number of params and another function that also accepts a variable number of paramaters, I need to forward all the variable paramaters in the method plus some extra to the second method that accepts variable paramaters ...
简而言之,我有一个函数接受可变数量的参数和另一个函数也接受可变数量的参数,我需要转发方法中的所有变量参数加上一些额外的接受变量参数的第二个方法。 。
action1(new Filter1());
action2(new Filter1(), new Filter2());
public void action1(Object ... params){
actionGeneric(new Action1(), params);
}
public void action2(Object ... params){
actionGeneric(new Action2(), params);
}
public void actionGeneric(Object ... params){
for (Object param : params){
if (param instanceof Action1){
// works fine
} else if (param instanceof Action2){
// works fine
} else if (param instanceof Filter1){
// never gets here
} else if (param instanceof Filter2){
// never gets here
}
}
}
So my question is, how should I be forwarding those params in action1 / action2 in order for actionGeneric to see them as Filter1 / Filter2 instances and not (I'm guessing) array type?
所以我的问题是,我应该如何在action1 / action2中转发这些参数,以便actionGeneric将它们视为Filter1 / Filter2实例而不是(我猜)数组类型?
2 个解决方案
#1
3
In the action2()
method, you call the actionGeneric()
method with two arguments: new Action2()
and params
(the latter of which is an Object array). Thus, from the point of view of the actionGeneric()
method, some arguments are of type Action2
but not Filter1
. The solution, I think, would be to create a new array before passing it to the actionGeneric()
method, along the lines of...
在action2()方法中,使用两个参数调用actionGeneric()方法:new Action2()和params(后者是Object数组)。因此,从actionGeneric()方法的角度来看,一些参数的类型为Action2但不是Filter1。我认为,解决方案是在将数组传递给actionGeneric()方法之前创建一个新数组。
Object[] newParams = new Object[params.length + 1];
newParams[0] = new Action1();
System.arraycopy(params, 0, newParams, 1, params.length);
actionGeneric(newParams);
#2
1
If a single array is passed to a variable-argument method, it will be treated as variable arguments. So, create a new array like this:
如果将单个数组传递给变量参数方法,则将其视为变量参数。所以,创建一个像这样的新数组:
Object[] newArray = new Object[params.length + 1];
newArray[0] = new Action1();
System.arraycopy(params, 0, newArray, 1, params.length);
actionGeneric(newArray);
#1
3
In the action2()
method, you call the actionGeneric()
method with two arguments: new Action2()
and params
(the latter of which is an Object array). Thus, from the point of view of the actionGeneric()
method, some arguments are of type Action2
but not Filter1
. The solution, I think, would be to create a new array before passing it to the actionGeneric()
method, along the lines of...
在action2()方法中,使用两个参数调用actionGeneric()方法:new Action2()和params(后者是Object数组)。因此,从actionGeneric()方法的角度来看,一些参数的类型为Action2但不是Filter1。我认为,解决方案是在将数组传递给actionGeneric()方法之前创建一个新数组。
Object[] newParams = new Object[params.length + 1];
newParams[0] = new Action1();
System.arraycopy(params, 0, newParams, 1, params.length);
actionGeneric(newParams);
#2
1
If a single array is passed to a variable-argument method, it will be treated as variable arguments. So, create a new array like this:
如果将单个数组传递给变量参数方法,则将其视为变量参数。所以,创建一个像这样的新数组:
Object[] newArray = new Object[params.length + 1];
newArray[0] = new Action1();
System.arraycopy(params, 0, newArray, 1, params.length);
actionGeneric(newArray);