Object类如何实现clone()方法

发布于 2024-10-24 04:12:10 字数 362 浏览 1 评论 0原文

在一本关于 Core Java 的书中,我发现了这样的摘录:

思考一下 对象类可以实现克隆。它 对物体一无所知, 所以它只能逐个字段 复制。如果对象中的所有数据字段 是数字或其他基本类型, 复制字段就可以了。但 如果该对象包含对 子对象,然后复制字段 给你另一个参考 子对象,所以原来的和 克隆对象仍然共享一些 信息。

读完这篇文章后,我想知道Object类中的clone方法最初是如何实现的?

令我困扰的是:Object类中的方法如何通过以下方式创建一个字段:子类对象的字段克隆,当它对该类一无所知时?

In a book on Core Java, I found this excerpt :

Think about the way in which the
Object class can implement clone. It
knows nothing about the object at all,
so it can make only a field-by-field
copy. If all data fields in the object
are numbers or other basic types,
copying the fields is just fine. But
if the object contains references to
subobjects, then copying the field
gives you another reference to the
subobject, so the original and the
cloned objects still share some
information.

After reading this I was wondering, that How is the clone method originally implemented in Object Class?

What bothers me is that: how can a method in Object class make a field by field clone of a sub-class object, when it knows nothing about that class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魂牵梦绕锁你心扉 2024-10-31 04:12:10

实际上,clone()是在本机代码中实现的,所以我假设它只是进行内存复制(复制所有字节)而不知道内容。

除此之外,还有 Reflection API 来获取有关类的知识(但是速度会更慢)。

Actually, clone() is implemented in native code, so I assume it just does a memory copy (copy all the bytes) without knowing the contents.

Besides that, there is the Reflection API to gain knowlegde about a class (which would be slower, however).

早乙女 2024-10-31 04:12:10

阅读此内容Javadoc

受保护的对象克隆() -

创建并返回此副本
目的。 “复制”的准确含义
可能取决于对象的类别。
一般意图是,对于任何
对象x,表达式:

x.clone() != x

将为真,并且表达式:

x.clone().getClass() == x.getClass()

是真的,但这些不是
绝对的要求。虽然它是
通常情况是:
x.clone().equals(x) 将为 true,这
不是绝对要求。经过
约定,返回的对象应该
通过调用super.clone获取。如果
一个类及其所有超类
(对象除外)遵守此约定,
情况将会是这样

x.clone().getClass() == x.getClass()。

按照约定,返回的对象
这个方法应该独立于
这个对象(正在被克隆)。

为了实现这种独立性,它
可能需要修改一项或多项
返回对象的字段
在返回之前进行 super.clone 。
通常,这意味着复制任何
可变对象包括
内部“深层结构”
对象被克隆并替换
对这些对象的引用
对副本的引用。如果一个班
仅包含原始字段或
对不可变对象的引用,然后
通常情况是没有字段
在 super.clone 返回的对象中
需要修改。

意味着当你的对象中有一个子对象时,你不应该仅仅克隆/复制它的引用,而应该克隆/复制该对象的内部结构(为了创建它的新实例),如果每个对象都有它的clean clone() 方法,您将能够像父对象一样克隆它,否则您将必须创建它的新实例并一一复制其内部预设字段。

Read this from the Javadoc:

protected Object clone() -

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 x, the expression:

x.clone() != x

will be true, and that the expression:

x.clone().getClass() == x.getClass()

will be true, but these are not
absolute requirements. While it is
typically the case that:
x.clone().equals(x) will be true, this
is not an absolute requirement. By
convention, the returned object should
be obtained by calling super.clone. If
a class and all of its superclasses
(except Object) obey this convention,
it will be the case that

x.clone().getClass() == x.getClass().

By convention, the object returned by
this method should be independent of
this object (which is being cloned).

To achieve this independence, it
may be necessary to modify one or more
fields of the object returned by
super.clone before returning it.
Typically, this means copying any
mutable objects that comprise the
internal "deep structure" of the
object being cloned and replacing the
references to these objects with
references to the copies. If a class
contains only primitive fields or
references to immutable objects, then
it is usually the case that no fields
in the object returned by super.clone
need to be modified.

Means when you have a subobject in your object you shouldnt just clone/copy its reference but the internal structure of this object (in order to create a new instance of it), if each object has its clean clone() methode you will be able to clone it like the parent object otherwise you will have to create a new instance of it and copy its internal premitive fields one by one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文