您好,欢迎访问一九零五行业门户网

java中的浅拷贝与深拷贝

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中的浅拷贝与深拷贝的详细内容。
其它类似信息

推荐信息