设计模式==原型模式(ProtoType)
/* * 原型模式(ProtoType) * * 通过一个原型对象来创建一个新对象(克隆)。Java中要给出Clonable接口的实现,具体类要实现这个接口,并给出clone()方法的实现细节,这就是简单原型模式的应用。 浅拷贝:只拷贝简单属性的值和对象属性的地址 深拷贝:拷贝本对象引用的对象,有可能会出现循环引用的情况。可以用串行化解决深拷贝。写到流里再读出来,这时会是一个对象的深拷贝结果。 * * */ package model; import java.io.Serializable; public class TestClonealbe { public static void main(String[] args) throws Exception { Father f = new Father(); User u1 = new User("123456", f); User u2 = (User) u1.clone(); System.out.println(u1 == u2); System.out.println(u1.f == u2.f); } } class User implements Cloneable, Serializable { private static final long serialVersionUID = 1L; String password; Father f; public User(String password, Father f) { this.password = password; this.f = f; } public Object clone() throws CloneNotSupportedException { return super.clone(); } } class Father implements Serializable { private static final long serialVersionUID = 1L; }
相关推荐
zhongshish 2020-10-21
jinfeng0 2020-08-03
uileader 2020-07-18
jameszgw 2020-06-21
yanglin 2020-05-02
钟鼎 2020-05-01
嵌入式移动开发 2020-04-10
THEEYE 2020-03-26
嵌入式移动开发 2020-01-23
gougouzhang 2020-01-12
TingBen 2019-12-29
chvnetcom 2020-01-06
Mrwind 2019-11-03
chvnetcom 2019-10-29
baohuanlove 2019-08-16
nongshuqiner 2015-07-27
anqier 2019-07-01
titans 2017-06-03
THEEYE 2016-12-22