是否可以在字符串中声明 Java 中的默认参数? [英] Is it possible to declare default argument in Java in String?

查看:12
本文介绍了是否可以在字符串中声明 Java 中的默认参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

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

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

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

public void test(String name)
{

}

这篇关于是否可以在字符串中声明 Java 中的默认参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆