简体   繁体   中英

Calling default constructor from another constructor

I am supposed to create a triangle object using 3 points given by the user, and if the triangle isn't legal then create the default one. Is this a "legal" way to create the default one using the default method, or is this a weird scenario that isn't a way people do things?

public class Triangle

private Point _point1;
private Point _point2;
private Point _point3;

public Triangle()
{
    _point1 = new Point(1,0);
    _point2 = new Point(-1,0);
    _point3 = new Point(0,1);
}

public Triangle(Point point1,Point point2,Point point3)
{
    if (isLegal(point1,point2,point3))
        {
        _point1 = new Point(point1);
        _point2 = new Point(point2);
        _point3 = new Point(point3);
        }
    else
        new Triangle();
}

You are allowed to have multiple constructors, and one constructor can call another. This is called constructor chaining.

Use this() within a constructor to refer to another constructor for the same class. Use super() to refer to a constructor of the superclass. The arguments you pass to this or super decide which constructor gets called.

Create a primary constructor that takes all the arguments you need. Then you can have secondary constructors that call another constructor, passing in default values.

The call to this() or super() has to be the first statement in the constructor. This is a problem for what you want to do. Handling validation separately from construction is probably better.

You could solve your problem like this (code cleaned up a little):

public class Triangle {
    private final Point point1;
    private final Point point2;
    private final Point point3;

    public Triangle() {
        this(new Point(1, 0), new Point(-1, 0), new Point(0, 1));
    }

    public Triangle(Point point1, Point point2, Point point3) {
        if (!isLegal(point1, point2, point3)) {
            throw new IllegalArgumentException("Points are illegal");
        }
        this.point1 = point1;
        this.point2 = point2;
        this.point3 = point3;
        // or if you need a copy constructor
        // this.point1 = new Point(point1);
        // this.point2 = new Point(point2);
        // this.point3 = new Point(point3);
    }

    public static boolean isLegal(Point point1, Point point2, Point point3) {
        // some magic calculations
        return result;
    }
}

and finally create your Triangle like this with your existing points p1 , p2 , 3 :

final Triangle t = Triangle.isLegal(p1, p2, p3) ? new Triangle(p1, p2, p3) : new Triangle();

or

Triangle t;
if(Triangle.isLegal(p1, p2, p3)) {
    t = new Triangle(p1, p2, p3);
} else {
    t = new Triangle();
}

The static method isLegal is used to check your parameters before the constructor is called. The check inside the constructor ensures that the constructor with the three points is not used with illegal arguments. If the given points are "illegal" the exception is thrown and quits your application if it is not caught somewhere ( Google: java exception ).

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