我阅读了《Effective Java》一书,但不理解解释可克隆接口(interface)的一段。谁能给我解释一下这一段:
...programmers assume that if they extend a class and invoke
super.clone
from the subclass, the returned object will be an instance of the subclass. The only way a superclass can provide this functionality is to return an object obtained by callingsuper.clone
. If a clone method returns an object created by a constructor, it will have the wrong class.
谢谢。
最佳答案
首先我要注意 clone
本身是坏的,而像 Sheep(Sheep cloneMe)
这样的复制构造函数是一个更优雅的习惯用法比clone
,考虑到Cloneable
合约非常弱。您可能已经知道这一点,因为您正在阅读这本书,但值得将其放在这里。
无论如何,回答这个问题:
Object.clone()
将创建一个与调用它的对象类型相同的对象。出于这个原因,强烈建议“级联”到 Object
以获得您计划返回的结果。如果有人决定不遵循这个约定,你最终会得到一个违反约定的类类型的对象,这会导致很多问题。
为了说明我有这样一个类
class Sheep implements Cloneable {
Sheep(String name)...
public Object clone() {
return new Sheep(this.name); // bad, doesn't cascade up to Object
}
}
class WoolySheep extends Sheep {
public Object clone() {
return super.clone();
}
}
突然间,如果我这样做
WoolySheep dolly = new WoolySheep("Dolly");
WoolySheep clone = (WoolySheep)(dolly.clone()); // error
我会得到一个异常,因为我从 dolly.clone()
返回的是一个 Sheep
,而不是一个 WoolySheep
。
关于java - 有效的Java。可克隆界面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11654176/