2

我构建了一个具有此方法的程序,并且我还想计算我创建的对象的数量。但是运行程序后。它说有0个对象正在创建。谁能知道为什么这是不正确的?应该说有 4 个对象被创建。这是我的代码:

    /**
     This program implements code for a Circle class,
     which has a radius field, set and
     get methods, and a getArea method.
     Author: Michael Wu.
    */

    public class Circle
    {
        private double radius;
        private static int numCircles;

        public Circle(double radius)
        {
            this.radius = radius;
        }

        //SetRadius method,sets radius.
        public void setRadius(double radius)
        {
            this.radius = radius;
        }

        //GetRadius method; returns radius.
        public double getRadius()
        {
            return radius;
        }

        //Constructor increments numbers of circles.
        public Circle()
        {
            numCircles++;
        }

        //Copy constuctor.
        public Circle(Circle c3)
        {
            radius = c3.radius;
        }

        //GetNumbercircles method; get number of circles.
        public int getNumCircles()
        {
            return numCircles;
        }

        //Copy method, copy objects.
        public Circle copy()
        {
            Circle copyObject = new Circle(radius);

            return copyObject;
        }

        //Call the getArea method.
        public double getArea()
        {
            return radius*radius*Math.PI;
        }
    }

    /**
     This program created several circle objects and then their areas and radius will be displayed on the screen.
     Author: Michael Wu.
    */

    public class CircleDemo
    {
        public static void main(String[] args)
        {
            int numCircles;

            //Create two circle objects.
            Circle c1 = new Circle(3.7);
            Circle c2 = new Circle(5.9);
            Circle c3 = new Circle(c1);

            //Declare a circle object. 
            Circle c4;

            //Make c3 reference a copy of a object refferenced by c1.
            c4 = c2.copy();

            //Display outputs.
            System.out.println("The radius for circle1 is "+ c1.getRadius());
            System.out.println("The area for circle1 is "+ c1.getArea());

            System.out.println("The radius for circle2 is "+ c2.getRadius());
            System.out.println("The area for circle2 is "+ c2.getArea());

            System.out.println("The radius for circle3 is "+ c3.getRadius());
            System.out.println("The area for circle3 is "+ c3.getArea());

            System.out.println("The radius for circle4 is "+ c4.getRadius());
            System.out.println("The area for circle4 is "+ c4.getArea());

            //Get the number of circles.
            numCircles = c1.getNumCircles();

            System.out.println("There are "+numCircles+" objects being created.");
        }
    }
4

5 回答 5

6

numCircles只有在调用Circle不带参数的构造函数时才会增加。但是你永远不会调用那个特定的构造函数;您调用其他构造函数。

所有构造函数的增量numCircles

顺便说一句,因为numCirclesis static,所以返回这个数字的方法getNumCircles也应该是static,您可以按如下方式调用它Circle.getNumCircles()

于 2013-06-28T21:07:55.080 回答
3

您为创建对象而调用的构造函数(带参数的构造函数)不会调用递增计数器的无参数构造函数。尝试:

public Circle(double radius)
{
    this();
    this.radius = radius;
}

public Circle(Circle c3)
{
    this();
    radius = c3.radius;
}

您必须显式调用,this()否则构造函数将隐式调用super(),在您的情况下是Object构造函数。

于 2013-06-28T21:11:22.380 回答
2

正如其他答案所提到的,您应该首先在每个构造函数中调用您的零参数构造this()函数,以确保它被执行。目前,您的代码不会调用零参数构造函数中的代码,因为它从不直接调用,也不会在调用其他构造函数时调用。

或者,您可以改为在声明下方添加一个内联初始化块,例如:

private static int numCircles = 0;
// Increment numCircles for every object constructed.
{
    numCircles++;
}

这是不太标准的,可能是更糟糕的做法,但它会自动确保其执行,而无需在任何构造函数中增加 numCircles。

于 2013-06-28T21:18:01.287 回答
0

你应该用 0 初始化numCircles并在每次构造函数调用时增加它,特别是因为你有多个构造函数你必须跟踪它们都是递增的numCircles。你可以从另一个构造函数内部调用一个构造函数,所以你不能向每个构造函数添加相等的语句。

于 2013-06-28T21:10:33.793 回答
0

移动你的代码'numCircles++;' 到 Circle(double radius) 构造函数。创建圆形对象时不调用 Circle()

public Circle(double radius)
{
    this.radius = radius;
    numCircles++;
}
于 2013-06-28T21:16:51.483 回答