java - 如何在我的类中使用复制构造函数和静态字段?
问题描述
我有一个问题,如何使用 finalize 方法创建静态字段来计算内存中给定类的对象数(受保护的 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());
}
}
解决方案
以下是供您参考的答案:
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
}
}
推荐阅读
- elasticsearch - Elasticsearch 嵌套路径查询到对象类型
- javascript - 无法在 Javascript 中使用 php 变量
- javascript - 将 prompt-sync 要求转换为导入方法
- c# - 如何以编程方式禁用 Word 开始屏幕
- twilio - Twilio 现在支持子流吗?
- ubuntu - 文件描述符 3(管道:[34331])在 vgs 调用时泄露。父 PID 5424:/usr/sbin/grub-probe
- python - 如何从 dir() 的变量中获取值
- spring - 添加 Springs LDAP-Core 后终止
- typescript - 在 Typescript 中使用枚举作为泛型类型时不兼容的类型
- mysql - 如何在 Sequelize 模型中动态选择数据库(或“模式”)?