原型模式:
原型模式,创建属性相同的对象,但是属于不同对象。通过实现Cloneable。重写clone方法。
package prototype;
public class Prototype implements Cloneable{
private String name;
private int age;
public Prototype(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return super.toString();
}
@Override
protected Object clone(){
Prototype clone=null;
try {
clone = (Prototype) super.clone();
}catch (CloneNotSupportedException e){
System.out.println(e.getMessage());
}
return clone;
}
}
获取属性相同的不同内存空间的对象。