ホームページ >Java >&#&チュートリアル >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の浅いコピーと深いコピーの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。