简体   繁体   English

有多个可选参数

[英]Have multiple optional parameters

I am trying to have optional parameters in my method. 我试图在我的方法中有可选参数。 I found the bolean... test, and it works. 我找到了bolean ...测试,它的确有效。 But whenever I try with a sencond one, it doesn't work. 但每当我尝试使用秒级时,它都不起作用。

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){}

Java does not support optional parameters in functions. Java不支持函数中的可选参数。

Instead, provide an overload to 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. 当然,可以通过这种方式默认多个参数,并且可以有多个重载来支持不同的默认方案。

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 null for 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可以定义传递哪个参数,哪个被省略!

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. Java支持重载方法,Java可以区分具有不同方法签名的方法。 This means that methods within a class can have the same name if they have different parameter lists: 这意味着如果类中的方法具有不同的参数列表,则它们可以具有相同的名称:

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方法的更多信息。

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

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 like this; 如果你需要这样的事;

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进入方法,它们最终将被解释为数组。

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])
{

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM