- 具体对象类定义
public class Prototype implements Cloneable {
//普通字段
private String subject;
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
@Override
protected Prototype clone() {
Prototype prototype=null;
try {
prototype= (Prototype) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return prototype;
}
}
- 使用
//原型模板对象
Prototype prototype = new Prototype();
//通过原型模板对象clone后对象
Prototype clonePrototype = prototype.clone();
clonePrototype.setSubject("subject");
网友评论