java中的浅拷贝与深拷贝
1、什么叫Java浅拷贝?
浅拷贝是会将对象的每个属性进行依次复制,但是当对象的属性值是引用类型时,实质复制的是其引用,当引用指向的值改变时也会跟着变化。
2、什么叫Java深拷贝?
深拷贝复制变量值,对于引用数据,则递归至基本类型后,再复制。深拷贝后的对象与原来的对象是完全隔离的,互不影响,对一个对象的修改并不会影响另一个对象。
相关视频教程推荐:java在线视频
3、Java浅拷贝和深拷贝的区别是什么?
通俗来讲浅拷贝的复制其引用,当引用指向的值改变时也会跟着变化;而深拷贝则是与原来的对象完全隔离,互补影响。
4、思维导图
5、测试用例分析
浅拷贝测试用例
publicclassShallowExperience {
privateString skill;
publicvoid setSkill(String skill) {
this.skill = skill;
}
publicvoid setShallowExperience(String skill) {
this.skill = skill;
}
@Override
publicString toString() {
returnskill;
}
}
publicclassShallowCloneTest implementsCloneable {
privateint age;
privateShallowExperience shallowExperience;
publicShallowCloneTest() {
this.age = 10;
this.shallowExperience = newShallowExperience();
}
publicShallowExperience getExperience() {
returnshallowExperience;
}
publicvoid setShallowExperience(String skill) {
shallowExperience.setShallowExperience(skill);
}
publicvoid show() {
System.out.println(shallowExperience.toString());
}
publicint getAge() {
returnage;
}
@Override
protectedObject clone() throws CloneNotSupportedException {
return(ShallowCloneTest) super.clone();
}
}
publicclassTestMain {
publicstaticvoid main(String[] args) throws CloneNotSupportedException {
System.out.println("======浅拷贝======");
shallowCloneTest();
}
/**
* 浅拷贝测试用例
*
* @throws CloneNotSupportedException
*/
privatestaticvoid shallowCloneTest() throws CloneNotSupportedException {
ShallowCloneTest test = newShallowCloneTest();
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());
}
}
深拷贝测试用例
publicclassDeepExperience implementsCloneable{
privateString skill;
publicvoid setSkill(String skill) {
this.skill = skill;
}
publicvoid setDeepExperience(String skill) {
this.skill = skill;
}
@Override
publicString toString() {
returnskill;
}
@Override
protectedObject clone() throws CloneNotSupportedException {
returnsuper.clone();
}
}
publicclassDeepCloneTest implementsCloneable {
privateint age;
privateDeepExperience deepExperience;
publicDeepCloneTest() {
this.age = 10;
this.deepExperience = newDeepExperience();
}
publicDeepExperience getExperience() {
returndeepExperience;
}
publicvoid setDeepExperience(String skill) {
deepExperience.setDeepExperience(skill);
}
publicvoid show() {
System.out.println(deepExperience.toString());
}
publicint getAge() {
returnage;
}
@Override
protectedObject clone() throws CloneNotSupportedException {
DeepCloneTest deepCloneTest = (DeepCloneTest) super.clone();
deepCloneTest.deepExperience = (DeepExperience) deepCloneTest.getExperience().clone();
returndeepCloneTest;
}
}
publicclassTestMain {
publicstaticvoid main(String[] args) throws CloneNotSupportedException {
System.out.println("======深拷贝======");
deepCloneTest();
}
/**
* 深拷贝测试用例
*
* @throws CloneNotSupportedException
*/
privatestaticvoid deepCloneTest() throws CloneNotSupportedException {
DeepCloneTest test = newDeepCloneTest();
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零基础入门
以上就是java中的浅拷贝与深拷贝的详细内容,更多请关注php中文网其它相关文章!