Constructor 解释为什么需要两个构造函数

Constructor 解释为什么需要两个构造函数,constructor,default,Constructor,Default,不幸的是,我对默认构造函数的理解没有信心。 我已经进行了广泛的搜索,以找到一个资源,该资源提供了遵守我个人Java语言学习曲线的解释。然而,在完成一项任务后,我觉得我可能不符合任务标准,因为我自己觉得需要一个默认构造函数是多余的。这就是为什么我觉得我误解了不同类型的构造函数的概念。 我已经根据任务的需要创建了两个构造函数。不接受任何参数并将实例变量初始化为默认值的函数。另一种方法是,当在main方法中创建新对象时,接收参数为对象变量提供值。 如果主方法中从未使用默认构造函数,为什么要为对象创建默

不幸的是,我对默认构造函数的理解没有信心。 我已经进行了广泛的搜索,以找到一个资源,该资源提供了遵守我个人Java语言学习曲线的解释。然而,在完成一项任务后,我觉得我可能不符合任务标准,因为我自己觉得需要一个默认构造函数是多余的。这就是为什么我觉得我误解了不同类型的构造函数的概念。 我已经根据任务的需要创建了两个构造函数。不接受任何参数并将实例变量初始化为默认值的函数。另一种方法是,当在main方法中创建新对象时,接收参数为对象变量提供值。 如果主方法中从未使用默认构造函数,为什么要为对象创建默认构造函数?下面是我的代码示例:

public class Circle {
    private double x;   // declaring variable to hold value of x coordinate
    private double y;   // Variable to hold value of y coordinate
    private double r;   // Variable to hold value of the radius of a circle

    /* default constructor */

    Circle() {
        x = 0.0;
        y = 0.0;
        r = 0.0;
    }

     /* constructor takes in three parameters and sets values for variables x, y, and r */

    public Circle(double x, double y, double r) {
        this.x = x;
        this.y = y;
        this.r = r;
    }

 // test class created for main method

public class TestCircle {
    public static void main (String[] args){
        Circle c1 = new Circle(2.0,3.0,9.0);
        System.out.println();
        System.out.println(" A circle object has been created with the following attributes:");
        c1.printAttributes();
        System.out.println();
        System.out.println("The circle is tested for the maximum radius of 8.0...");
    c1.setRadius(8.0);
    System.out.println();
    System.out.println("... since the radius is more than the allowable maximum, the new attributes for the Circle are:");
    c1.printAttributes();
    System.out.println();
    System.out.println("The area of the Circle is " + c1.area());
    System.out.println("The Circumference of the circle is " + c1.circumference());
    System.out.println();
    System.out.println("The origin of the circle is now moved by a specified amount...");
    c1.move(6,-7);
    System.out.println();
    System.out.println("The new attributes of the circle are:");
    c1.printAttributes();
    System.out.println();
    System.out.println("Testing if the point (10,-20) is inside the circle...");
    System.out.println();
    if (c1.isInside(10,-20)){
        System.out.println("The point (10,-20) is inside the circle");
    }
    else {
        System.out.println("The point (10,-20) is not inside the circle");
    }
} // end of main

}//课程结束

如果你不使用它,你应该删除它。有时您需要创建空对象来设置后验属性,但如果您根本不使用它,那么就没有必要使用它

创建默认构造函数的目的有时是为了后端,这被认为是一种“良好的编程实践”不,您在main中不使用默认构造函数,事实上,您的代码在没有默认构造函数的情况下运行得很好,请将其注释掉,然后重新运行测试程序,您将看到它工作得很好

与后端或良好实践无关,如果开发人员提供了任何构造函数,那么就不会有默认构造函数。是的,Sami是对的,我不好。至于与后端或良好实践无关,我不同意,因为如果您想在子级中使用super(),那么您确实需要在中使用默认构造函数,并且最好是创建默认构造函数,以便希望创建子级的人可以使用super()。也许这不一定是后端,但这绝对是一个好的实践,因为它允许更容易扩展的代码。不,这不是任何实践。子类调用它们需要或想要的构造函数。由于子类的原因,创建一个需要构造函数中的参数才能具有无参数构造函数的类没有很好的实践。如果没有所需的参数,它会做什么?对不起,我只是在鹦鹉学舌地模仿我的一位教授,在这学期之前,我实际上持有相反的观点,但我只是假设我的教授有行业经验,知道得更清楚,事实并非总是如此。