📜  java代码示例中的复制构造函数

📅  最后修改于: 2022-03-11 14:52:33.083000             🧑  作者: Mango

代码示例1
public class CopyConstructorExample {
  
    private int someInt;
      private String someString;
    private Date someDate;
  
      public CopyConstructorExample(CopyConstructorExample example) {
        this.someInt = example.getSomeInt();
        this.someString = example.getSomeString();
          this.someDate = new Date(example.getSomeDate().getTime()); 
          // We need to construct a new Date object, else we would have the same date object
    }
  
  // Other Constructors here
  // Getters and Setters here
}
public class UsageExample {
    public static void main(String[] args) {
        CopyConstructorExample example = new CopyConstructorExample(5, "Test");
          // Copy example using the Constructor
          CopyConstructorExample copy = new CopyConstructorExample(example);
          
    }
}