深复制与浅复制也看了有好几次了,今天碰到了具体问题时竟然还是不知道应该怎样解决,所以在学习这块一定要少说些主义,多研究些问题。
我对深复制浅复制的理解:深复制和浅复制被复制对象都含有与原来对象相同的值,不同点是:浅复制中复制后的对象中的引用都仍然指向原来的对象,深复制中赋值后的对象中的引用指向新的对象,即浅复制时仅仅复制所考虑的对象,不复制它所引用的对象,深复制则不同,把要复制对象所引用的对象也复制了一次。为了验证上面的话分别进行以下浅复制和深复制
浅复制:
class Grant implements Cloneable {
String deffend;
int attack;
public BaladeMaster b;
Grant(String deffend, int attack, BaladeMaster b) {
this.deffend = deffend;
this.attack = attack;
this.b = b;
}
public Grant clone() {
Grant o = null;
try {
o = (Grant) super.clone();
} catch (CloneNotSupportedException e) {
System.out.println(e.toString());
}
<span style="color:#ff6666;">/*o.b = (BaladeMaster) this.b.clone(); */</span>
return o;
}
}
public class BaladeMaster implements Cloneable {
public int grade;
public String skill;
public BaladeMaster(int grade, String skill) {
this.grade = grade;
this.skill = skill;
}
<span style="color:#ff6666;">/*public BaladeMaster clone() {
BaladeMaster b = null;
try {
b = (BaladeMaster) super.clone();
} catch (CloneNotSupportedException e) {
System.out.print(e.toString());
}
return b;
}*/</span>
public static void main(String[] args) {
BaladeMaster b = new BaladeMaster(3, "跳劈");
Grant g1 = new Grant("一防", 18, b);
g1.b = b;
Grant g2 = (Grant) g1.clone();
//深复制浅复制中的变量拷贝和原版中的一样
System.out.println("deffend=" + g1.deffend + "," + "attack=" + g1.attack);//
System.out.println("deffend=" + g2.deffend + "," + "attack=" + g2.attack);
System.out.println(g1.b==g2.b);
//浅复制修改copy中的变量对原版中的有影响
g2.attack = 19;
g2.deffend = "两防";
System.out.println("deffend=" + g1.deffend + "," + "attack=" + g1.attack);//
System.out.println("deffend=" + g2.deffend + "," + "attack=" + g2.attack);
//浅复制修改copy中的引用的对象,原版中的引用对象也跟着改变,说明clone()为浅复制
g2.b.skill = "疾风步";
System.out.println(g1.b.skill);
System.out.println(g2.b.skill);
}
}
输出:
deffend=一防,attack=18
deffend=一防,attack=18
true
deffend=一防,attack=18
deffend=两防,attack=19
疾风步
疾风步
深复制:
class Grant implements Cloneable {
String deffend;
int attack;
public BaladeMaster b;
Grant(String deffend, int attack, BaladeMaster b) {
this.deffend = deffend;
this.attack = attack;
this.b = b;
}
public Grant clone() {
Grant o = null;
try {
o = (Grant) super.clone();
} catch (CloneNotSupportedException e) {
System.out.println(e.toString());
}
o.b = (BaladeMaster) this.b.clone();
return o;
}
}
public class BaladeMaster implements Cloneable {
public int grade;
public String skill;
public BaladeMaster(int grade, String skill) {
this.grade = grade;
this.skill = skill;
}
public BaladeMaster clone() {
BaladeMaster b = null;
try {
b = (BaladeMaster) super.clone();
} catch (CloneNotSupportedException e) {
System.out.print(e.toString());
}
return b;
}
public static void main(String[] args) {
BaladeMaster b = new BaladeMaster(3, "跳劈");
Grant g1 = new Grant("一防", 18, b);
g1.b = b;
Grant g2 = (Grant) g1.clone();
//深复制中的变量拷贝和原版中的一样
System.out.println("deffend=" + g1.deffend + "," + "attack=" + g1.attack);//
System.out.println("deffend=" + g2.deffend + "," + "attack=" + g2.attack);
System.out.println(g1.b==g2.b);
//修改copy中的变量对原版中的有影响
g2.attack = 19;
g2.deffend = "两防";
System.out.println("deffend=" + g1.deffend + "," + "attack=" + g1.attack);//
System.out.println("deffend=" + g2.deffend + "," + "attack=" + g2.attack);
//深复制修改copy中的引用的对象,原版中的引用对象就不改变了
g2.b.skill = "疾风步";
System.out.println(g1.b.skill);
System.out.println(g2.b.skill);
}
}
输出:
deffend=一防,attack=18
deffend=一防,attack=18
false
deffend=一防,attack=18
deffend=两防,attack=19
跳劈
疾风步
对比两段代码:
深浅复制在代码上区别只有上文中红色部分。
浅复制只是用了clone()函数对对象进行复制,并且为浅复制。深复制中时被引用的类中也写明了clone()函数并声明为public (不然会提示引用调调用时会有方法不存在错误,因为基类中的clone()函数为protected)