Java 有多个可选参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22227407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Have multiple optional parameters
提问by 3751_Creator
I am trying to have optional parameters in my method. I found the bolean... test, and it works. But whenever I try with a sencond one, it doesn't work.
我试图在我的方法中使用可选参数。我找到了 bolean... 测试,它有效。但是每当我尝试使用第二个时,它都不起作用。
Is there a possibility to put two or more (of same type eg: 2 option booleans)
是否有可能放置两个或更多(相同类型,例如:2 个选项布尔值)
Code: What I have now:
代码:我现在拥有的:
public void addJButton(boolean... yo){}
What I want:
我想要的是:
public void addJButton(boolean... yo, boolean... yo2){}
采纳答案by piet.t
As for the varargs-notation (boolean...): the varargs-parameter always has to be the last one, so you can only have one of these.
至于 varargs-notation ( boolean...):varargs-parameter 必须是最后一个,所以你只能拥有其中之一。
You can consider passing nullfor omitted parameters or you could try some sort of method-overloading like Bathseba suggested.
您可以考虑传递null省略的参数,或者您可以尝试像 Bathseba 建议的某种方法重载。
When going for the overloading you have to keep in mind that there are no named paramters, so only poosition and type can define which parameter is passed and which is omitted!!
在进行重载时,您必须记住没有命名参数,因此只有 poosition 和 type 可以定义哪些参数被传递,哪些被省略!!
回答by Bathsheba
Java does not support optional parameters in functions.
Java 不支持函数中的可选参数。
Instead, provide an overloadto the function like this:
相反,为该函数提供一个重载,如下所示:
void myFunction(boolean parameter)
{
/*ToDo - code here*/
}
void myFunction()
{
myFunction(true/*i.e. call the overload with a default to true*/);
}
Of, course, more than one parameter can be defaulted in this way and you can have multiple overloads to support different default schemes.
当然,通过这种方式可以默认多个参数,并且您可以有多个重载来支持不同的默认方案。
回答by Hari
Java does support optional parameters in the form of var-args, but each method can only have 1 var-arg parameter and it must be the last in the list of parameters ( Varargs).
Java 确实支持 var-args 形式的可选参数,但每个方法只能有 1 个 var-arg 参数,并且必须是参数列表 ( Varargs) 中的最后一个。
Java supports overloading methods, and Java can distinguish between methods with different method signatures. This means that methods within a class can have the same name if they have different parameter lists:
Java 支持重载方法,Java 可以区分具有不同方法签名的方法。这意味着如果类中的方法具有不同的参数列表,则它们可以具有相同的名称:
public class DataArtist {
...
public void draw(boolean b1) {
...
}
public void draw(boolean b1, boolean b2) {
...
}
public void draw(double f) {
...
}
public void draw(int i, double f) {
...
}
}
More Info on: Java Methods.
更多信息:Java 方法。
回答by Rudi Kershaw
This is a bit of a late answer but Java has var-args instead of optional parameters and as others have said you can only use one per method, and the var-arg parameter must be the last in the method.
这有点晚了,但 Java 有 var-args 而不是可选参数,正如其他人所说,每个方法只能使用一个,并且 var-arg 参数必须是方法中的最后一个。
If you need something likethis;
如果你需要的东西像这样;
public void addJButton(boolean... yo, boolean... yo2){}
Then the following alternatives are available.
然后可以使用以下替代方案。
public void addJButton(boolean yo1, boolean yo2, boolean... yo3){}
public void addJButton(boolean[] yo1, boolean[] yo2){}
The first option means manually specifying the number of booleans in an overloaded method, and the second option takes two arrays of booleans. Your var-args will ultimately be interpreted as arrays once they enter the method anyway.
第一个选项意味着在重载方法中手动指定布尔值的数量,第二个选项需要两个布尔值数组。无论如何,您的 var-args 最终将被解释为数组,一旦它们进入该方法。
回答by rahulrv
Try the builder pattern when handling multiple or optional parameters
处理多个或可选参数时尝试构建器模式
// Builder Pattern
public class NutritionFacts {
private final int servingSize;
private final int servings;
private final int calories;
private final int fat;
private final int sodium;
private final int carbohydrate;
public static class Builder {
// Required parameters
private final int servingSize;
private final int servings;
// Optional parameters - initialized to default values
private int calories = 0;
private int fat = 0;
private int carbohydrate = 0;
private int sodium = 0;
public Builder(int servingSize, int servings) {
this.servingSize = servingSize;
this.servings = servings;
}
public Builder calories(int val)
{ calories = val; return this; }
public Builder fat(int val)
{ fat = val; return this; }
public Builder carbohydrate(int val)
{ carbohydrate = val; return this; }
public Builder sodium(int val)
{ sodium = val; return this; }
public NutritionFacts build() {
return new NutritionFacts(this);
}
}
private NutritionFacts(Builder builder) {
servingSize = builder.servingSize;
servings = builder.servings;
calories = builder.calories;
fat = builder.fat;
sodium = builder.sodium;
carbohydrate = builder.carbohydrate;
}
}
Adding values :
添加值:
NutritionFacts cocaCola = new NutritionFacts.Builder(240, 8).
calories(100).sodium(35).carbohydrate(27).build();
回答by kudzai zishumba
If you want to have multiple optional parameters
如果你想有多个可选参数
public void initDatePickers(final EditText et,boolean... disableFuturedates)
{...}
you can then access the values as an array like so,
然后你可以像这样以数组的形式访问这些值,
if(disableFuturedates[0])
{
}

