抽象原型 Prototype.java
java
public abstract class Prototype implements Cloneable{ protected String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override protected Object clone() throws CloneNotSupportedException { // TODO Auto-generated method stub return super.clone(); } }
具体原型 ConcretePrototype.javaide
public class ConcretePrototype extends Prototype { }
测试类 Test.java测试
public class Test { public static void main(String[] args) throws CloneNotSupportedException { Prototype p = new ConcretePrototype(); p.setName("zhangsan"); System.out.println(p.getName()); Prototype p2 = (Prototype) p.clone(); System.out.println(p2.getName()); } }
测试结果:this
zhangsan zhangsan