《设计模式系列》---克隆模式
生活随笔
收集整理的這篇文章主要介紹了
《设计模式系列》---克隆模式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
淺復制:被復制對象的所有變量都含有與原來的對象相同的值,而所有的對其他對象的引用都仍然指向原來的對象.
/***?@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?blocke.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?blocke.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();}}轉載于:https://my.oschina.net/stefanzhlg/blog/308136
總結
以上是生活随笔為你收集整理的《设计模式系列》---克隆模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 打包bat等文件成exe,双击运行不显示
- 下一篇: 11条javascript知识