public class Student implements Cloneable{
private Date birthday;
public Student() {}
public Student(Date birthday) {
this.birthday = birthday;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class CloneTest {
@Test
public void test1() throws CloneNotSupportedException {
Student student1 = new Student(new Date());
Student student2 = (Student) student1.clone();
System.out.println(student1 == student2);
}
}
public class Student implements Cloneable{
private Date birthday;
public Student() {}
public Student(Date birthday) {
this.birthday = birthday;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
protected Object clone() throws CloneNotSupportedException {
Student student = (Student) super.clone();
student.birthday = (Date) student.birthday.clone();
return student;
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |