![](/img/trans.png)
[英]Why when implementing clone method we return super.clone() not this clone()
[英]Understanding what happens when we override the clone method with and without invoking super.clone?
我正在阅读Joshua Bloch撰写的Effective Java。 我必须说这是一本密集而复杂的书。 事实证明,对于我来说,不到3年的编程时间(在Java中为1年),对所有对象通用的方法(第3章)这一章非常困难。 我不太了解适当重写克隆方法的概念。 我可以得到一个简单易懂的示例来实现克隆,正确和错误的方法吗? 为什么不能调用super.clone会导致问题? 会发生什么?
先感谢您。
我自己在读书。 不知道在这个例子中我是否做对了所有事情,但是也许会对您有所帮助。
Computer.java
package testclone;
public class Computer implements Cloneable {
String OperatingSystem;
protected Computer Clone() throws CloneNotSupportedException {
Computer newClone = (Computer) super.clone();
newClone.OperatingSystem = this.OperatingSystem;
return newClone;
}
}
MultiCore.java
package testclone;
public class MultiCore extends Computer implements Cloneable {
int NumberOfCores;
@Override
protected MultiCore Clone() throws CloneNotSupportedException {
//********* use 1 of the next 2 lines ***********
//MultiCore newClone = (MultiCore) super.clone();
MultiCore newClone = new MultiCore();
newClone.NumberOfCores = this.NumberOfCores;
return newClone;
}
}
TestClone.java
package testclone;
public class TestClone implements Cloneable {
public static void main(String[] args) throws CloneNotSupportedException {
//Computer myComputer = new Computer();
//myComputer.OperatingSystem = "Windows";
MultiCore myMultiCore = new MultiCore();
myMultiCore.OperatingSystem = "Windows"; //field is in parent class
myMultiCore.NumberOfCores = 4;
MultiCore newMultiCore = myMultiCore.Clone();
System.out.println("orig Operating System = " + myMultiCore.OperatingSystem);
System.out.println("orig Number of Cores = " + myMultiCore.NumberOfCores);
System.out.println("clone Operating System = " + newMultiCore.OperatingSystem);
System.out.println("clone Number of Cores = " + newMultiCore.NumberOfCores);
}
}
输出:
orig操作系统= Windows
orig核心数= 4
clone操作系统= null *此行不是您想要的。
克隆核心数= 4
如果改用super.clone()行,则输出为
orig操作系统= Windows
orig核心数= 4
clone操作系统= Windows *现在就是您想要的
克隆核心数= 4
因此,如果您不使用super.clone(),它将不会在父级(或祖父母级或曾祖父母级等)中克隆字段
祝好运! (抱歉-上面键入的内容看起来格式正确,但是由于实际原因,它看起来很糟糕)
您应该始终使用super.clone()
。 如果不这样做,请说return new MyObject(this.x);
,那么对于MyObject实例来说效果很好。 但是,如果有人扩展了MyObject,则在覆盖clone
方法时,他们将再也无法获得正确的类的实例。 Object.clone所做的一件事情是您无法做好自己的工作,即创建了正确的类的实例。 其余的只是复制实例字段,这是繁琐的工作,您可以根据需要自己完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.