Constructors with default values of parameters

This question already has an answer here:

  • Does Java support default parameter values? 19 answers

  • No, Java doesn't support default values for parameters. You can overload constructors instead:

    public Shape(int v,int e) {vertices =v; edges = e; }
    public Shape() { this(1, 2); }
    

    No it doesn't. Java doesn't support default arguments in any function; constructors included.

    What you can do though is define public Shape(int v, int e) and also a default constructor

    public Shape()
    {
        this(1, 2);
    }
    

    Note the special syntax here to delegate the construction to the two-argument constructor.

    链接地址: http://www.djcxy.com/p/20868.html

    上一篇: Java替代C#代码

    下一篇: 具有参数默认值的构造函数