Java初试原型模式-Cloneable
1 public class RestrationInfo implements Cloneable { 2 3 private String name; 4 private String birthday; 5 private String school; 6 private String ID; 7 //有参构造函数 8 public RestrationInfo(String name) { 9 this.name = name; 10 } 11 //设置生日 12 public void setBirthday(String birthday) { 13 this.birthday = birthday; 14 } 15 //设置毕业院校 16 public void setSchool(String school) { 17 this.school = school; 18 } 19 //设置身份证号 20 public void setID(String iD) { 21 ID = iD; 22 } 23 public void Show() { 24 System.out.println("姓名"+name); 25 System.out.println("出生年月"+birthday); 26 System.out.println("毕业院校"+school); 27 System.out.println("身份证号"+ID); 28 } 29 //实现Cloneable接口的clone方法 30 @Override 31 public Object clone() { 32 try { 33 return super.clone(); 34 }catch(CloneNotSupportedException e) { 35 return null; 36 } 37 38 39 } 40 public static void main(String[] args) { 41 //报名面点师 42 System.out.println("报名面点师"); 43 RestrationInfo restrationInfo = new RestrationInfo("小面"); 44 restrationInfo.setBirthday("1887-8-6"); 45 restrationInfo.setSchool("dax"); 46 restrationInfo.setID("123456"); 47 //展示报名信息 48 restrationInfo.Show(); 49 //报名一级厨师 50 System.out.println("报名一级厨师"); 51 RestrationInfo restrationInfo1 = (RestrationInfo)restrationInfo.clone(); 52 //展示报名信息 53 restrationInfo1.Show(); 54 } 55 }
输出: