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);

更多相关文章

  1. Java中怎么把字符串数组转为整形数组
  2. 剑指Offer(六)旋转数组的最小数字(Java版 )
  3. 准备语句SQL异常“无输入参数”
  4. Java中double型数组的HashCode产生
  5. 求助:json + java 返回 数据 数组中去掉双引号
  6. 了解数组类型和使用java.util.Arrays类
  7. java的数组下标的非常严重的问题,异常不爽
  8. java数组常用功能
  9. 对一个JavaScript对象数组进行排序。

随机推荐

  1. Android拍照、录像、录音代码范例
  2. Android倒计时实现
  3. 2013.11.19 ——— android 获取本地ip地
  4. Android中的onTouch事件
  5. Android 强制停止RecyclerView滑动方法
  6. NDK各版本下载
  7. Android RecyclerView 去掉滑动边缘阴影
  8. Android应用自启动
  9. Android 框架类图
  10. android 6.0编译时出现ERROR:Security pro