简体   繁体   English

从另一个构造函数调用默认构造函数

[英]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.我应该使用用户给出的 3 个点创建一个三角形 object,如果三角形不合法,则创建默认三角形。 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.在构造函数中使用 this() 来引用相同 class 的另一个构造函数。 Use super() to refer to a constructor of the superclass.使用 super() 来引用超类的构造函数。 The arguments you pass to this or super decide which constructor gets called.您传递给此的 arguments 或超级决定调用哪个构造函数。

Create a primary constructor that takes all the arguments you need.创建一个主构造函数,它包含您需要的所有 arguments。 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() 或 super() 的调用必须是构造函数中的第一条语句。 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 :最后用你现有的点p1p23创建你的Triangle

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. static 方法isLegal用于在调用构造函数之前检查您的参数。 The check inside the constructor ensures that the constructor with the three points is not used with illegal arguments.构造函数内部的检查确保了具有三个点的构造函数没有与非法的 arguments 一起使用。 If the given points are "illegal" the exception is thrown and quits your application if it is not caught somewhere ( Google: java exception ).如果给定点是“非法的”,则抛出异常并退出您的应用程序,如果它没有在某处被捕获(谷歌:java 异常)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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