将方法的参数传递给另一个方法?

时间:2012-02-01 02:51:52

标签: java multithreading methods parameters arguments

我有以下代码:

public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    // Do stuff
}

我希望能够做到这一点:

public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    someMethod(sameParameters);
}

我该怎么做?你能提供一个例子吗?

3 个答案:

答案 0 :(得分:7)

不是你想要的,但这是如何:

public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    someMethod(sender, cmd, commandLabel, args);
}

答案 1 :(得分:1)

这样做的显而易见的方法是简单地将所有接收的参数作为参数传递。也就是说,

public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    someMethod(sender, cmd, commandLabel, args);
}

答案 2 :(得分:1)

variadicWikipedia方法可能看起来更好:

void foo(Object... params) {
    // access like params[0], for example
    delegateFoo(params);
}

void delegateFoo(Object... params) {
    // params[0] is same as above
}