简体   繁体   中英

meaning of invoking default constructor

I have a homework already done, my teacher get back to me said I need to invoking the default constructor, I am not quite understand what is the difference, can anyone help to explain it to me? Thanks

here is my original code

public Triangle()
    {
        this.side1 = 1.0;
        this.side2 = 1.0;
        this.side3 = 1.0;
    }

and the code that my teacher want me to change

public Triangle()
    {
        this(1.0,1.0,1.0);
    }

Assuming that you already have a constructor like this:

public Triangle(double s1, double s2, double s3) {
    this.side1 = s1;
    this.side1 = s1;
    this.side1 = s1;
}

then your two examples are functionally identical. However it is always good practice to call this constructor instead of repeating yourself with another three assignments.


As a side note, "invoking the default" constructor is referring to invoking the constructor with no parameters, either through creating an instance or calling this() through another constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM