如何在类中使用复制构造函数和静态字段?

gkn4icbw  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(411)

我有一个问题,如何使用finalize方法(protectedvoidfinalize()throws throwable)创建静态字段来计算内存中给定类的对象数?第二个问题,我是否在这个类中很好地创建了复制构造函数,例如,如果没有,我应该怎么做?

public Rectangle(Rectangle point){
width = point.width
height = point.height

}
public class Point {
        public int x = 0;
        public int y = 0;
        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public Point(int x) {
            this.x = x;
        }

    }
    class Rectangle {
        public int width = 0;
        public int height = 0;
        Point origin;
        // four constructors
        public Rectangle() {
            origin = new Point(0, 0);
        }
        public Rectangle(Point p) {
            origin = p;
        }
        public Rectangle(int w, int h) {
            this(new Point(0, 0), w, h);
        }
        public Rectangle(Point p, int w, int h) {
            origin = p;
            width = w;
            height = h;
        }
        public void move(int x, int y) {
            origin.x = x;
            origin.y = y;
        }
        public int area() {

            return width * height;
        }
    }

    public class exa_1 {
        public static void main(String[] args) {
            Rectangle myRect = new Rectangle(5,10);
            myRect.width = 40;
            myRect.height = 50;
            System.out.println("Area: " + myRect.area());
        }
    }
mfuanj7w

mfuanj7w1#

以下是答案供您参考:
1.)对内存中的对象进行计数
可以创建静态类变量。然后,在构造函数中添加计数,并在finalize函数中减少计数器。
2.)对于复制构造函数,我们需要一个深度副本,因为矩形有一个可变的对象。此外,还需要计算内存中的对象。
备注:不建议使用finalize(),因为它在Java9中已被弃用。以下是不使用它的原因:
a、 )不保证在finalize()中执行。当jvm调用finalize()时,您可能会遇到这样的情况:jvm过早退出,垃圾收集器没有足够的时间创建和执行终结器。
b、 )终结是一个复杂的过程,通常会导致性能问题、死锁和挂起。这就是为什么java也反对它。
c、 )finalize()方法不能像构造函数那样在链接中工作。子类finalize()不调用超类'finalize()。
d、 )finalize方法引发的任何异常都会导致暂停此对象的终结,但在其他情况下会被忽略。它甚至不会登录到您的日志文件中。如果出了问题,调试几乎是不可能的。

class Point {
    public static int count = 0;
    public int x = 0;
    public int y = 0;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
        count++;  //Add the object count
        System.out.println("Point constructor. object count="+ count);
    }

    public Point(int x) {
        this(x,0);
    }

    @Override
    protected void finalize() {
        count--;  //reduce the object count
        System.out.println("Point finalize. object count="+ count);
    }

}
class Rectangle {

    public static int count = 0;
    public int width = 0;
    public int height = 0;
    Point origin;

    public Rectangle() {
        this(new Point(0, 0));
    }
    public Rectangle(Point p) {
        this(p,0,0);
    }
    public Rectangle(int w, int h) {
        this(new Point(0, 0), w, h);
    }
    public Rectangle(Point p, int w, int h) {
        origin = p;
        width = w;
        height = h;
        count++;  //Add the object count
        System.out.println("Rectangle constructor. object count="+ count);
    }

    //Copy constructor
    public Rectangle(Rectangle rectangle) {

        this(new Point(rectangle.getPoint().x, rectangle.getPoint().y), rectangle.width, rectangle.height);
    }

    public void move(int x, int y) {
        origin.x = x;
        origin.y = y;
    }
    public int area() {

        return width * height;
    }

    public Point getPoint() {

        return origin;
    }

    @Override
    protected void finalize() {
        count--;  //reduce the object count
        System.out.println("Rectangle finalize. object count="+ count);
    }
}

public class exa_1 {
    public static void main(String[] args) {
        Rectangle myRect = new Rectangle(5,10);
        myRect.width = 40;
        myRect.height = 50;
        System.out.println("Area: " + myRect.area());

        Rectangle myRect2 = new Rectangle(5,10);
        myRect2.width = 60;
        myRect2.height = 70;
        System.out.println("Area: " + myRect2.area());

        Rectangle myRect3 = new Rectangle(myReact2);
        System.out.println("Area: " + myRect3.area());

        myReact = null;  //Set to null so that the object will be removed during gc
        System.gc(); //to clear the memory
    }
}

相关问题