Java能设置参数默认值吗
不能,死心吧,只能使用可变参数,如下:
@Test
public void test1() {
var("张3","打球","吃饭");
var("张3");
}
//默认爱好,相当于默认值
static final String default_hobby = "打球";
static void var(String name, String... hobby) {
if (hobby.length == 0) {
//hobby参数默认值
System.out.println(name + "喜欢" + default_hobby);
} else {
System.out.println(name + "喜欢" + Stream.of(hobby).collect(Collectors.joining()));
}
}
Loading...