package org.ustc.prototype;
public class Resume implements Cloneable {
private String name;
private String gender;
private int age;
private Birthday birth;
public Birthday getBirth() {
return birth;
}
public void setBirth(Birthday birth) {
this.birth = birth;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
protected Resume clone() {
// TODO Auto-generated method stub
Resume clone =null;
try {
clone = (Resume) super.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
clone.birth = (Birthday) this.birth.clone();//生拷贝 钱拷贝
return clone;
}
@Override
public String toString() {
return "Resume [name=" + name + ", gender=" + gender + ", age=" + age
+ ", birth=" + birth + "]";
}
}
package org.ustc.prototype;
public class Birthday implements Cloneable{
private int year;
private int month;
private int day;
public Birthday(int year, int month, int day) {
super();
this.year = year;
this.month = month;
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
@Override
public String toString() {
return "Birthday [year=" + year + ", month=" + month + ", day=" + day
+ "]";
}
@Override
protected Object clone() {
// TODO Auto-generated method stub
Object clone = null;
try{
clone = super.clone();
}catch(CloneNotSupportedException e){
e.printStackTrace();
}
return clone;
}
}
package org.ustc.prototype;
public class Main {
public static void main(String[] args) {
Resume res = new Resume();
res.setAge(30);
res.setGender("male");
res.setName("大鸟");
Birthday birth = new Birthday(1999,1,1);
res.setBirth(birth);
Resume res1 = (Resume) res.clone();
res1.getBirth().setDay(50);
//res1.setBirth(new Birthday(1928, 2, 2));
//res1.setBirth(birth);
System.out.println(res1);
System.out.println(res);
}
}