无法计算对象数量

时间:2013-06-28 21:06:08

标签: java

我构建了一个具有此方法的程序,我也想要 计算我创建的对象数量。但是在运行程序之后。它说有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.");
        }
    }

5 个答案:

答案 0 :(得分:6)

只有在没有参数的情况下调用numCircles构造函数时,才会增加Circle。但你永远不会称那个特定的构造函数;你打电话给其他建设者。

在所有构造函数上增加numCircles

顺便说一下,因为numCirclesstatic,返回此数字的方法getNumCircles也应该是static,您可以按如下方式调用它:{{1} }。

答案 1 :(得分:3)

您正在调用以创建对象(带参数的对象)的构造函数不会调用增加计数器的no-arg构造函数。尝试:

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

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

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

答案 2 :(得分:2)

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

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

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

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

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

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