Java clone()方法不会在类unimplemented Cloneable的对象上引发调用RuntimeException
测试用例(jdk版本:oracle 1.6.031) 错误在哪里?根据: 因此,永远不会对类调用方法Java clone()方法不会在类unimplemented Cloneable的对象上引发调用RuntimeException,java,clone,core,Java,Clone,Core,测试用例(jdk版本:oracle 1.6.031) 错误在哪里?根据: 因此,永远不会对类调用方法clone() 此外,如果您想从对象中的clone()方法的行为中获益,并在对象未实现Cloneable时引发异常,则应在类的重写方法clone中调用super.clone()。
clone()
此外,如果您想从对象中的clone()
方法的行为中获益,并在对象未实现Cloneable
时引发异常,则应在类的重写方法clone
中调用super.clone()
。<
因此,永远不会对类调用方法clone()
此外,如果您想从对象中的clone()
方法的行为中获益,并在对象未实现Cloneable
时引发异常,您应该在类的重写方法clone
中调用super.clone()
。在使用clone时?一般来说,克隆是有点破碎的。我的意思是,这是我在使用克隆时犯的错误还是jdk中的一些错误?在使用克隆时?一般来说,克隆是有点破碎的。我的意思是,这是我在使用克隆方面的错误还是jdk中的一些错误?谢谢!正确的测试用例将是公共类TestCloneable{public TestCloneable clone()抛出CloneNotSupportedException{TestCloneable tc=(TestCloneable)super.clone();返回tc;}}谢谢!正确的测试用例将是公共类TestCloneable{public TestCloneable clone()抛出CloneNotSupportedException{TestCloneable tc=(TestCloneable)super.clone();返回tc;}
public class TestCloneable{
public TestCloneable clone(){
return new TestCloneable();
}
}
public static void main(String[] args) {
TestCloneable testObj = new TestCloneable();
TestCloneable testObj2 = new TestCloneable();
System.out.println(testObj.clone());
Hashtable<Integer, TestCloneable> ht = new Hashtable<Integer, TestCloneable>();
ht.put(1, testObj);
ht.put(2, testObj2);
System.out.println(ht.clone());
HashMap<Integer, TestCloneable> hm = new HashMap<Integer, TestCloneable>();
hm.put(1, testObj);
hm.put(2, testObj2);
System.out.println(hm.clone());
}
/**
* @exception CloneNotSupportedException if the object's class does not
* support the Cloneable
interface. Subclasses ...
*/
clone()
Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned.