浅复制:被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象.
/** * @author stefanie zhao * @date 2014-8-15 下午02:31:04 */ public class Resume implements Cloneable { private String name; private String sex; private String age; private WorkExperience work; public Resume(String name) { this.name = name; work = new WorkExperience(); } public void setPersonalInfo(String sex, String age) { this.sex = sex; this.age = age; } public void setWorkExperience(String workDate, String company) { work.setCompany(company); work.setWorkDate(workDate); } public Object clone() { Object re = null; try { return (Object) super.clone(); } catch (CloneNotSupportedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return re; } public void dispay() { System.out.format("%s %s %s", name, sex, age); System.out.format("工作经历:%s %s", work.getWorkDate(), work.getCompany()); } }
/** * @author stefanie zhao * @date 2014-8-15 下午02:29:23 */ public class WorkExperience { private String workDate; private String company; /** * @return the workDate */ public String getWorkDate() { return workDate; } /** * @param workDate * the workDate to set */ public void setWorkDate(String workDate) { this.workDate = workDate; } /** * @return the company */ public String getCompany() { return company; } /** * @param company * the company to set */ public void setCompany(String company) { this.company = company; } }
public class Main { /** * 浅复制:被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象. * * @Description: TODO * @param @param args * @return void * @throws */ public static void main(String[] args) { Resume a = new Resume("A"); a.setPersonalInfo("man", "28"); a.setWorkExperience("2009-2011", "xx company"); Resume b = (Resume) a.clone(); b.setWorkExperience("2011-2013", "yy company"); Resume c = (Resume) a.clone(); c.setWorkExperience("2011-2013", "zz company"); a.dispay(); b.dispay(); c.dispay(); } }
深复制把引用对象的变量指向复制过来的新的对象,而不是原来的被引用的对象。
/** * @author stefanie zhao * @date 2014-8-15 下午02:31:04 */ public class Resume implements Cloneable { private String name; private String sex; private String age; private WorkExperience work; public Resume(String name) { this.name = name; work = new WorkExperience(); } public Resume(WorkExperience work) { this.work = (WorkExperience) work.clone(); } public void setPersonalInfo(String sex, String age) { this.sex = sex; this.age = age; } public void setWorkExperience(String workDate, String company) { work.setCompany(company); work.setWorkDate(workDate); } public Object clone() { Resume re = new Resume(this.work); re.setPersonalInfo(this.sex, this.age); return re; } public void dispay() { System.out.format("%s %s %s", name, sex, age); System.out.format("工作经历:%s %s", work.getWorkDate(), work.getCompany()); } }
/** * @author stefanie zhao * @date 2014-8-15 下午02:29:23 */ public class WorkExperience implements Cloneable { private String workDate; private String company; /** * @return the workDate */ public String getWorkDate() { return workDate; } /** * @param workDate * the workDate to set */ public void setWorkDate(String workDate) { this.workDate = workDate; } /** * @return the company */ public String getCompany() { return company; } /** * @param company * the company to set */ public void setCompany(String company) { this.company = company; } public Object clone() { Object re = null; try { return (Object) super.clone(); } catch (CloneNotSupportedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return re; } }
public class Main { /** * 深复制把引用对象的变量指向复制过来的新的对象,而不是原来的被引用的对象。 * * @Description: TODO * @param @param args * @return void * @throws */ public static void main(String[] args) { Resume a = new Resume("A"); a.setPersonalInfo("man", "28"); a.setWorkExperience("2009-2011", "xx company"); Resume b = (Resume) a.clone(); b.setWorkExperience("2011-2013", "yy company"); Resume c = (Resume) a.clone(); c.setWorkExperience("2011-2013", "zz company"); a.dispay(); b.dispay(); c.dispay(); } }