【问题标题】:Is it possible to declare default argument in Java in String? [duplicate]是否可以在 String 中声明 Java 中的默认参数? [复制]
【发布时间】:2014-05-30 08:59:36
【问题描述】:

是否可以在 String 的方法中使用默认参数。代码如下:

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

上面的代码产生错误。可以改正吗?

【问题讨论】:

标签: java methods default-value


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    不,您通常这样做的方式是像这样重载方法:

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

    【讨论】:

      猜你喜欢
      • 2021-08-06
      • 2020-10-27
      • 1970-01-01
      • 2012-12-01
      • 2018-06-13
      • 1970-01-01
      • 2020-01-27
      • 2015-07-21
      • 1970-01-01
      相关资源
      最近更新 更多