我正在阅读answer,他提到了link,作者在其中解释了为什么我们不应该使用Cloneable.但是,仍然怀疑那里所说的是什么
If I have an array of Cloneable, you would think that I could run down
that array and clone every element to make a deep copy of the array,
but I can’t. You can not cast something to Cloneable and call the clone
method, because Cloneable doesn’t have a public clone method and
neither does Object. If you try to cast to Cloneable and call the
clone method, the compiler will say you are trying to call the
protected clone method on object.
但是,我在这里做了
Init s = Init.getInstance(); // getting instance
int count=0;
Cloneable[] v = new Cloneable[5]; // creating array
Init x = s;
Init y = new Init(s);
try {
while (count < 5) {
v[count++] = (Cloneable) s.clone(); // casting it.
}
s.setClassName("Example");
System.out.println(((Init) v[2]).getClassName()); // Displaying.
} catch (CloneNotSupportedException ex) {
ex.printStackTrace();
}
我能够创建Cloneable数组,我做了作者所说的会导致错误的操作,还是我误解了作者的声明?任何人,请帮助我了解选择“复制构造函数”而不是“可复制”的原因.