Java Default Parameters Using Method Overloading – 使用方法重载的Java默认参数

最后修改: 2019年 12月 14日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this short tutorial, we’ll demonstrate the use of method overloading to simulate default parameters in Java.

在这个简短的教程中,我们将演示使用方法重载来模拟Java中的默认参数。

Here, we say simulate because unlike certain other OOP languages (like C++ and Scala), the Java specification doesn’t support assigning a default value to a method parameter.

在这里,我们说模拟,是因为与其他某些OOP语言(如C++和Scala)不同,Java规范不支持为方法参数分配默认值

2. Example

2.例子

As an example, let’s make some tea! First, we’ll need a Tea POJO:

作为一个例子,让我们做一些茶!首先,我们需要一个Tea POJO。

public class Tea {

    static final int DEFAULT_TEA_POWDER = 1;

    private String name; 
    private int milk;
    private boolean herbs;
    private int sugar;
    private int teaPowder;

    // standard getters 
}

Here, the name is a required field, as our Tea has to have a name at least.

这里,name是一个必填字段,因为我们的Tea至少得有个名字。

Then, there cannot be any tea without tea powder. So, we’ll assume that the user wants a standard 1 tbsp teaPowder in their tea, if it’s not provided at invocation time. This then is our first default parameter.

那么,没有茶粉就不可能有茶。因此,我们将假设用户希望在他们的茶中加入标准的1汤匙茶粉,如果在调用时没有提供的话。这就是我们的第一个默认参数

The other optional parameters are milk (in ml), herbs (to add or not to add), and sugar (in tbsp). If any of their values are not provided, we assume the user doesn’t want them.

其他可选参数是牛奶(毫升)、草药(添加或不添加)和(汤匙)。如果没有提供它们中的任何一个值,我们就认为用户不想要它们。

Let’s see how to achieve this in Java using method overloading:

让我们看看如何在Java中使用方法重载来实现这一目标

public Tea(String name, int milk, boolean herbs, int sugar, int teaPowder) {
    this.name = name;
    this.milk = milk;
    this.herbs = herbs;
    this.sugar = sugar;
    this.teaPowder = teaPowder;
}

public Tea(String name, int milk, boolean herbs, int sugar) {
    this(name, milk, herbs, sugar, DEFAULT_TEA_POWDER);
}

public Tea(String name, int milk, boolean herbs) {
    this(name, milk, herbs, 0);
}

public Tea(String name, int milk) {
    this(name, milk, false);
}

public Tea(String name) {
    this(name, 0);
}

As is evident, here we’re using constructor chaining, a form of overloading to provide the method parameters with some default values.

很明显,这里我们使用了构造函数链,这是一种重载的形式,为方法参数提供一些默认值。

Now let’s add a simple test to see this in action:

现在让我们添加一个简单的测试来看看这个动作。

@Test
public void whenTeaWithOnlyName_thenCreateDefaultTea() {
    Tea blackTea = new Tea("Black Tea");

    assertThat(blackTea.getName()).isEqualTo("Black Tea");
    assertThat(blackTea.getMilk()).isEqualTo(0);
    assertThat(blackTea.isHerbs()).isFalse();
    assertThat(blackTea.getSugar()).isEqualTo(0);
    assertThat(blackTea.getTeaPowder()).isEqualTo(Tea.DEFAULT_TEA_POWDER);
}

3. Alternatives

3.替代品

There are other ways to achieve default parameter simulation in Java. Some of them are:

在Java中还有其他方法来实现默认参数模拟。其中一些是

Here’s how we can make use of the third way of allowing null arguments in our example:

下面是我们如何在我们的例子中利用第三种允许空参数的方式。

public Tea(String name, Integer milk, Boolean herbs, Integer sugar, Integer teaPowder) {
    this.name = name;
    this.milk = milk == null ? 0 : milk.intValue();
    this.herbs = herbs == null ? false : herbs.booleanValue();
    this.sugar = sugar == null ? 0 : sugar.intValue();
    this.teaPowder = teaPowder == null ? DEFAULT_TEA_POWDER : teaPowder.intValue();
}

4. Conclusion

4.总结

In this article, we looked at using method overloading to simulate default parameters in Java.

在这篇文章中,我们研究了使用方法重载来模拟Java中的默认参数。

While there are other ways to achieve the same, overloading is most clean and simple. As always, code is available over on GitHub.

虽然有其他方法可以实现同样的目的,但重载是最干净和简单的。像往常一样,代码可在GitHub上获得