Does Java support default parameter values?

后端 未结 25 1643
清歌不尽
清歌不尽 2020-11-22 07:07

I came across some Java code that had the following structure:

public MyParameterizedFunction(String param1, int param2)
{
    this(param1, param2, false);
}         


        
25条回答
  •  -上瘾入骨i
    2020-11-22 07:29

    No, but you can use the Builder Pattern, as described in this Stack Overflow answer.

    As described in the linked answer, the Builder Pattern lets you write code like

    Student s1 = new StudentBuilder().name("Eli").buildStudent();
    Student s2 = new StudentBuilder()
                     .name("Spicoli")
                     .age(16)
                     .motto("Aloha, Mr Hand")
                     .buildStudent();
    

    in which some fields can have default values or otherwise be optional.

提交回复
热议问题