抽象原型 Prototype.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.java
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()); }}
测试结果:
zhangsanzhangsan