1 /** 2 * 声明一个克隆自身的接口 3 * @author 4 * 5 */ 6 public class Prototype implements Cloneable { 7 8 private String name; 9 10 public void setName(String name) { 11 this.name = name; 12 } 13 14 public String getName() { 15 return this.name; 16 } 17 18 public Object clone(){ 19 try { 20 return super.clone(); 21 } catch (Exception e) { 22 e.printStackTrace(); 23 return null; 24 } 25 } 26 } 1 /** 2 * 实现一个克隆自身的操作 3 * @author 4 * 5 */ 6 public class ConcretePrototype extends Prototype { 7 8 public ConcretePrototype(String name) { 9 setName(name); 10 } 11 }
1 public class Test { 2 3 public static void main(String[] args) { 4 Prototype pro = new ConcretePrototype("prototype"); 5 Prototype pro2 = (Prototype)pro.clone(); 6 System.out.println(pro.getName()); 7 System.out.println(pro2.getName()); 8 } 9 }
看了一下大神写的,今天理解还是有点模糊,感觉就是克隆,方便建造实例。
每天进步,每天学习...
转载于:https://www.cnblogs.com/huzi007/p/3884994.html