wordpress更换背景/seo课程排行榜
Prototype, 克隆旧对象。工作中很少用。
Java中的原型模式
实现原型模式需要实现标记型接口 Cloneable。
一般会重写clone()方法。
如果只实现clone()方法,而没实现接口,调用时会抛出异常。
一般用于一个对象的属性已经确定,需要产生很多相同对象的时候。
class Person implements Cloneable {int age = 8;int score = 100;Location location = new Location();public Object clone() throws CloneNotSupportedException {Person person = (Person) super.clone();person.location = (Location) this.location.clone();return person;}
}class Location implements Cloneable{String street;public Object clone() throws CloneNotSupportedException {return super.clone();}
}