是否可以在字符串中声明 Java 中的默认参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23093327/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 20:38:49  来源:igfitidea点击:

Is it possible to declare default argument in Java in String?

javamethodsdefault-value

提问by user3455638

Is it possible to use default argument in the method with String. The code is shown below:

是否可以在带有字符串的方法中使用默认参数。代码如下所示:

public void test(String name="exampleText") {
}

The code above generate error. Is it possible to correct it?

上面的代码产生错误。是否有可能纠正它?

采纳答案by MrLore

No, the way you would normally do this is overload the method like so:

不,您通常这样做的方法是重载方法,如下所示:

public void test()
{
    test("exampleText");
}

public void test(String name)
{

}

回答by nanofarad

No, it is not. However, the following is possible:

不它不是。但是,以下是可能的:

public void test() {
    test("exampleText");
}
public void test(String name) {
    //logic here
}