Does Java support default parameter values?

后端 未结 25 2045
清歌不尽
清歌不尽 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条回答
  •  梦如初夏
    2020-11-22 07:27

    I might be stating the obvious here but why not simply implement the "default" parameter yourself?

    public class Foo() {
            public void func(String s){
                    func(s, true);
            }
            public void func(String s, boolean b){
                    //your code here
            }
    }
    

    for the default, you would either use

    func("my string");
    

    and if you wouldn't like to use the default, you would use

    func("my string", false);
    

提交回复
热议问题