Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有默认值的Java函数变量_Java - Fatal编程技术网

具有默认值的Java函数变量

具有默认值的Java函数变量,java,Java,我在java中有一些函数,我希望这个函数有一个默认值的参数,如果没有发送值,则设置为variable,如下所示: private void test(int count=5) { } 因此,我可以通过两种方式调用该函数: test()和测试(10) 我该怎么做?您可以有两个重载方法。没有参数的版本将调用另一个接受int输入的版本,并传递默认值5 private void test() { test(5); } private void test(int count) {

我在java中有一些函数,我希望这个函数有一个默认值的参数,如果没有发送值,则设置为variable,如下所示:

private void test(int count=5)
 {
 }
因此,我可以通过两种方式调用该函数:
test()和<代码>测试(10)


我该怎么做?

您可以有两个重载方法。没有参数的版本将调用另一个接受
int
输入的版本,并传递默认值5

private void test() {
    test(5);
}

private void test(int count) {
    // rest of method
}

使用重载。如果为空,则使用整数-->将变量初始化为5@LaurentB这仍然需要传递null。。?