java中的浅拷贝与深拷贝
Java  /  管理员 发布于 8年前   184
1、什么叫Java浅拷贝?
浅拷贝是会将对象的每个属性进行依次复制,但是当对象的属性值是引用类型时,实质复制的是其引用,当引用指向的值改变时也会跟着变化。
2、什么叫Java深拷贝?
深拷贝复制变量值,对于引用数据,则递归至基本类型后,再复制。深拷贝后的对象与原来的对象是完全隔离的,互不影响,对一个对象的修改并不会影响另一个对象。
相关视频教程推荐:java在线视频
3、Java浅拷贝和深拷贝的区别是什么?
通俗来讲浅拷贝的复制其引用,当引用指向的值改变时也会跟着变化;而深拷贝则是与原来的对象完全隔离,互补影响。
4、思维导图
5、测试用例分析
浅拷贝测试用例
public class ShallowExperience { private String skill; public void setSkill(String skill) { this.skill = skill; } public void setShallowExperience(String skill) { this.skill = skill; } @Override public String toString() { return skill; }}public class ShallowCloneTest implements Cloneable { private int age; private ShallowExperience shallowExperience; public ShallowCloneTest() { this.age = 10; this.shallowExperience = new ShallowExperience(); } public ShallowExperience getExperience() { return shallowExperience; } public void setShallowExperience(String skill) { shallowExperience.setShallowExperience(skill); } public void show() { System.out.println(shallowExperience.toString()); } public int getAge() { return age; } @Override protected Object clone() throws CloneNotSupportedException { return (ShallowCloneTest) super.clone(); }}public class TestMain { public static void main(String[] args) throws CloneNotSupportedException { System.out.println("======浅拷贝======"); shallowCloneTest(); } /** * 浅拷贝测试用例 * * @throws CloneNotSupportedException */ private static void shallowCloneTest() throws CloneNotSupportedException { ShallowCloneTest test = new ShallowCloneTest(); test.setShallowExperience("我是小明,我精通Java,C++的复制粘贴"); test.show(); ShallowCloneTest cloneTest = (ShallowCloneTest) test.clone(); cloneTest.show(); cloneTest.setShallowExperience("我是小明的副本,我精通Java,C++"); cloneTest.show(); test.show(); System.out.println(cloneTest.getAge()); }}//运行结果======浅拷贝======我是小明,我精通Java,C++的复制粘贴我是小明,我精通Java,C++的复制粘贴我是小明的副本,我精通Java,C++我是小明的副本,我精通Java,C++10
深拷贝测试用例
public class DeepExperience implements Cloneable{ private String skill; public void setSkill(String skill) { this.skill = skill; } public void setDeepExperience(String skill) { this.skill = skill; } @Override public String toString() { return skill; } @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); }}public class DeepCloneTest implements Cloneable { private int age; private DeepExperience deepExperience; public DeepCloneTest() { this.age = 10; this.deepExperience = new DeepExperience(); } public DeepExperience getExperience() { return deepExperience; } public void setDeepExperience(String skill) { deepExperience.setDeepExperience(skill); } public void show() { System.out.println(deepExperience.toString()); } public int getAge() { return age; } @Override protected Object clone() throws CloneNotSupportedException { DeepCloneTest deepCloneTest = (DeepCloneTest) super.clone(); deepCloneTest.deepExperience = (DeepExperience) deepCloneTest.getExperience().clone(); return deepCloneTest; }}public class TestMain { public static void main(String[] args) throws CloneNotSupportedException { System.out.println("======深拷贝======"); deepCloneTest(); } /** * 深拷贝测试用例 * * @throws CloneNotSupportedException */ private static void deepCloneTest() throws CloneNotSupportedException { DeepCloneTest test = new DeepCloneTest(); test.setDeepExperience("我是小明,我精通Java,C++的复制粘贴"); test.show(); DeepCloneTest cloneTest = (DeepCloneTest) test.clone(); cloneTest.show(); cloneTest.setDeepExperience("我是小明的副本,我精通Java,C++"); cloneTest.show(); test.show(); System.out.println(cloneTest.getAge()); }}//运行结果======深拷贝======我是小明,我精通Java,C++的复制粘贴我是小明,我精通Java,C++的复制粘贴我是小明的副本,我精通Java,C++我是小明,我精通Java,C++的复制粘贴10
相关文章教程推荐:java零基础入门
以上就是java中的浅拷贝与深拷贝的详细内容,更多请关注其它相关文章!
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号