* @return a clone of this instance. * @exception CloneNotSupportedException if the object's class does not * support the <code>Cloneable</code> interface. Subclasses * that override the <code>clone</code> method can also * throw this exception to indicate that an instance cannot * be cloned. * @see java.lang.Cloneable */ protected native Object clone() throws CloneNotSupportedException;
* @exception CloneNotSupportedException if the object's class does not
* support the <code>Cloneable</code> interface. Subclasses
* that override the <code>clone</code> method can also
* throw this exception to indicate that an instance cannot
* be cloned.
* @see java.lang.Cloneable
*/
protected native Object clone() throws CloneNotSupportedException;
只不是是用clone()是有提交的。
1、对象(如X)必须继承Cloneable
2、必须重载clone方法。
只不是是用clone()是有提交的。
1、对象(如X)必须继承Cloneable
2、必须重载clone方法。
--------------------------------------------------对于条件1,从代码可知 类X 已经implements了Cloneable 。对于条件2,其实“必须重载clone”只是java文档里希望程序员去做的事情,而java的继承机制的原理并没有要求必须重载clone,我上面代码中类A ,B ,C 的例子就证明了这一点。 现在要研究的核心问题是:Object这个类的proteced方法clone如何违反java的继承机制,让你在继承于Object的子类X中不重载该方法,你就无法在同样继承于Object的另一个子类Y中调用X的对象继承于Object类的clone方法的? 要实现这种约束,编译器似乎是已经擅自违反了java的继承原理中proteced方法的访问规则, 专门对Object这个类进行了特殊控制,强迫程序员不重载clone方法就别想调用它。