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

How can I use copy constructor and static fields in my class?

提问人:JoshSoHP 提问时间:11/24/2020 更新时间:11/25/2020 访问量:335

问:

我有一个问题,如何创建静态字段来计算给定类的对象数量 在内存中使用 finalize 方法 (protected void finalize () 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());
        }
    }
Java 对象 字段 复制构造函数

评论

0赞 NightEye 11/27/2020
如果您觉得对您有用,请随时接受并点赞下面的答案

答:

0赞 Monique G. 11/25/2020 #1

以下是供您参考的答案:

1.) 用于计算内存中的对象

  • 您可以创建静态类变量。然后,在构造函数中添加计数并减少 finalize 函数中的计数器。

2.) 对于复制构造函数,我们需要一个深度复制,因为 Rectangle 有一个可变对象。此外,您还需要将对象计算在 记忆。

备注:不建议使用 finalize(),因为它是 在 Java 9 中不推荐使用。以下是不使用它的原因:

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
    }
}