在哪里可以看到JAVA的object类中clone方法的源代码?

来源:百度知道 编辑:UC知道 时间:2024/05/27 16:52:23
这个方法是JAVA中的native
方法,不过我不清楚在本地调用中它具体是怎么实现的,给出一个例子就行!

看不到的,是C代码。听说Java开源了,你倒是可以去官网上看看。

调用中实现clonable接口并给出方法。

public class Example implements Clonable {

int valueToClone;

public Object clone() {
Object o = super.clone();
(Example) o).valueToClone = valueToClone;

return o;
}
}

另外参见API. 有的书建议不采用Clonable,因为非常难实现一个完美的Clone 链,且方法本身返回Object需要cast,并不美观。可以自己写一个接口代替。

这方法在Object中并未实现

/**
* Creates and returns a copy of this object. The precise meaning
* of "copy" may depend on the class of the object. The general
* intent is that, for any object <tt>x</tt>, the expression:
* <blockquote>
* <pre>
* x.clone() != x</pre></blockquote>
* will be true, and that the expression:
* <blockquote>
* <pre>
* x.clone().getClass() == x.getClass()</pre></blockquote><