转载自:http://www.java2000.net/p1235
如下的程序,我们克隆羊圈,每个羊圈里有一只羊
- package test.lang;
- public class Sheep implements Cloneable {
- private String name;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public Sheep clone() throws CloneNotSupportedException {
- return (Sheep) super.clone();
- }
- public static void main(String[] args) throws CloneNotSupportedException {
- Sheepfold sheepfold1 = new Sheepfold();
- sheepfold1.setName("第一个羊圈");
- Sheep sheep1 = new Sheep();
- sheep1.setName("第一只羊");
- sheepfold1.setSheep(sheep1);
- Sheepfold sheepfold2 = sheepfold1.clone();
- sheepfold2.setName("第一个羊圈的克隆");
- System.out.println("sheepfold1.name=" + sheepfold1.getName() + ",sheep.name=" + sheepfold1.getSheep().getName());
- System.out.println("sheepfold2.name=" + sheepfold2.getName() + ",sheep.name=" + sheepfold2.getSheep().getName());
- System.out.println("sheepfold1.sheep==sheepfold1.sheep?" + (sheepfold1.getSheep() == sheepfold2.getSheep()));
- }
- }
- class Sheepfold implements Cloneable {
- private String name;
- private Sheep sheep;
- @Override
- public Sheepfold clone() throws CloneNotSupportedException {
- Sheepfold o = (Sheepfold) super.clone();
- return o;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Sheep getSheep() {
- return sheep;
- }
- public void setSheep(Sheep sheep) {
- this.sheep = sheep;
- }
- }
package test.lang;
public class Sheep implements Cloneable {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public Sheep clone() throws CloneNotSupportedException {
return (Sheep) super.clone();
}
public static void main(String[] args) throws CloneNotSupportedException {
Sheepfold sheepfold1 = new Sheepfold();
sheepfold1.setName("第一个羊圈");
Sheep sheep1 = new Sheep();
sheep1.setName("第一只羊");
sheepfold1.setSheep(sheep1);
Sheepfold sheepfold2 = sheepfold1.clone();
sheepfold2.setName("第一个羊圈的克隆");
System.out.println("sheepfold1.name=" + sheepfold1.getName() + ",sheep.name=" + sheepfold1.getSheep().getName());
System.out.println("sheepfold2.name=" + sheepfold2.getName() + ",sheep.name=" + sheepfold2.getSheep().getName());
System.out.println("sheepfold1.sheep==sheepfold1.sheep?" + (sheepfold1.getSheep() == sheepfold2.getSheep()));
}
}
class Sheepfold implements Cloneable {
private String name;
private Sheep sheep;
@Override
public Sheepfold clone() throws CloneNotSupportedException {
Sheepfold o = (Sheepfold) super.clone();
return o;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Sheep getSheep() {
return sheep;
}
public void setSheep(Sheep sheep) {
this.sheep = sheep;
}
}
运行结果如下:
- sheepfold1.name=第一个羊圈,sheep.name=第一只羊
- sheepfold2.name=第一个羊圈的克隆,sheep.name=第一只羊
- sheepfold1.sheep==sheepfold1.sheep?true
sheepfold1.name=第一个羊圈,sheep.name=第一只羊
sheepfold2.name=第一个羊圈的克隆,sheep.name=第一只羊
sheepfold1.sheep==sheepfold1.sheep?true
克隆的羊圈,里面的羊还是第一个羊圈的羊,他们完全相同.并没有被克隆过来,这就是浅层克隆,我们调整代码,实现深层克隆
- @Override
- public Sheepfold clone() throws CloneNotSupportedException {
- Sheepfold o = (Sheepfold) super.clone();
- o.sheep = this.sheep.clone();
- return o;
- }
@Override
public Sheepfold clone() throws CloneNotSupportedException {
Sheepfold o = (Sheepfold) super.clone();
o.sheep = this.sheep.clone();
return o;
}
运行结果
- sheepfold1.name=第一个羊圈,sheep.name=第一只羊
- sheepfold2.name=第一个羊圈的克隆,sheep.name=第一只羊
- sheepfold1.sheep==sheepfold1.sheep?false