阿里云虚拟主机多个网站吗,wordpress手机端添加底部功能菜单,进行网站建设的上市公司,思明建设局网站原型模式核心复制#xff0c;每次new出来的对象完全不一样#xff0c;实现对象之间的隔离。 学习前最好先掌握jAVA值传递和深浅拷贝 设计模式#xff0c;一定要敲代码理解
浅拷贝
克隆出对象#xff0c;其中两者的引用类型属性是同一个对象。
对象信息
/*** author ggb…原型模式核心复制每次new出来的对象完全不一样实现对象之间的隔离。 学习前最好先掌握jAVA值传递和深浅拷贝 设计模式一定要敲代码理解
浅拷贝
克隆出对象其中两者的引用类型属性是同一个对象。
对象信息
/*** author ggbond* date 2024年04月03日 08:38*/
public class Mankind01 implements Cloneable {private int age;private Date birth;public Mankind01(int age, Date birth){this.ageage;this.birthbirth;}public int getAge() {return age;}public Date getBirth() {return birth;}public void setAge(int age) {this.age age;}public void setBirth(Date birth) {this.birth birth;}Overrideprotected Mankind01 clone() throws CloneNotSupportedException {return (Mankind01) super.clone();}
}测试
/*** author ggbond* date 2024年04月03日 08:42**/
public class test01 {public static void main(String[] args) throws Exception {Date birthnew Date(2022,4,3);int age25;Mankind01 m1new Mankind01(age,birth);Mankind01 m2m1.clone();System.out.println(m1.age:m1.getAge() m2.age:m2.getAge());System.out.println(m1.birth:m1.getBirth() m2.birth:m2.getBirth());System.out.println(-----------);age21; birth.setTime(1232321321L);System.out.println(m1.getBirth() m2.getBirth()); // trueSystem.out.println(m1.age:m1.getAge() m2.age:m2.getAge());System.out.println(m1.birth:m1.getBirth() m2.birth:m2.getBirth());}
}测试结果发现 m1,m2 中的属性 引用类型 Date birth 是指向同一个对象
m1.age:25 m2.age:25
m1.birth:Wed May 03 00:00:00 CST 3922 m2.birth:Wed May 03 00:00:00 CST 3922
-----------
true
m1.age:25 m2.age:25
m1.birth:Thu Jan 15 14:18:41 CST 1970 m2.birth:Thu Jan 15 14:18:41 CST 1970深拷贝
对象信息
/*** author ggbond* date 2024年04月03日 08:38*/
public class Mankind02 implements Cloneable {private int age;private Date birth;public Mankind02(int age, Date birth){this.ageage;this.birthbirth;}public int getAge() {return age;}public Date getBirth() {return birth;}public void setAge(int age) {this.age age;}public void setBirth(Date birth) {this.birth birth;}Overrideprotected Mankind02 clone() throws CloneNotSupportedException {Mankind02 m (Mankind02) super.clone();m.setBirth((Date) birth.clone());return m;}
}
测试
/*** author ggbond* date 2024年04月03日 08:42* 原型模式本质是克隆复制对象。*/
public class test02 {public static void main(String[] args) throws Exception {Date birthnew Date(2022,4,3);int age25;Mankind02 m1new Mankind02(age,birth);Mankind02 m2m1.clone();System.out.println(m1.age:m1.getAge() m2.age:m2.getAge());System.out.println(m1.birth:m1.getBirth() m2.birth:m2.getBirth());System.out.println(-----------);age21; birth.setTime(123232321L);System.out.println(m1.getBirth() m2.getBirth()); // trueSystem.out.println(m1.age:m1.getAge() m2.age:m2.getAge());System.out.println(m1.birth:m1.getBirth() m2.birth:m2.getBirth());}
}
结果
m1.age:25 m2.age:25
m1.birth:Wed May 03 00:00:00 CST 3922 m2.birth:Wed May 03 00:00:00 CST 3922
-----------
false
m1.age:25 m2.age:25
m1.birth:Fri Jan 02 18:13:52 CST 1970 m2.birth:Wed May 03 00:00:00 CST 3922总结
使用场景需要到大量相同对象但内部内容或结构可能进行修改。思考上述被复制对象中如果含多层引用克隆包含循环引用的复杂对象可能会非常麻烦。
代码下载
代码下载