了解如何复制数据

understanding how to copy data

我正在做一些入门级的作业 java,我 运行 参与其中。我不知道它要我做什么。这真的只是将“Date”设置为等于"Date d" 中的值吗?还是我错过了什么?我觉得一行代码不会用那么长的解释。

有人可以解释一下这里发生了什么以及我遗漏了什么吗?

Copy constructor: this is a constructor that accepts one parameter of type Date and then sets the receiving (or executing) objects instance variables equal to those of the parameter object. The result is that the receiving object is a copy of the formal parameter object:

public Date( Date d )

您需要做的就是获取 d 的所有字段并将它们复制到新日期。因此,如果日期有时间、日期、月份和年份,请将所有这些复制到新日期。就是这样。

class Complex {

private double re, im;

// A normal parametrized constructor 
public Complex(double re, double im) {
    this.re = re;
    this.im = im;
}

// copy constructor
Complex(Complex c) {
    System.out.println("Copy constructor called");
    re = c.re;
    im = c.im;
}

// Overriding the toString of Object class
@Override
public String toString() {
    return "(" + re + " + " + im + "i)";
  }
}

public class Main {

public static void main(String[] args) {
    Complex c1 = new Complex(10, 15);

    // Following involves a copy constructor call
    Complex c2 = new Complex(c1);   

    // Note that following doesn't involve a copy constructor call as 
    // non-primitive variables are just references.
    Complex c3 = c2;   

    System.out.println(c2); // toString() of c2 is called here
  }
}