In a book on Core Java, I found this excerpt :
在一本关于Core Java的书中,我发现了这个摘录:
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.
考虑一下Object类可以实现克隆的方式。它根本不了解对象,因此它只能进行逐个字段的复制。如果对象中的所有数据字段都是数字或其他基本类型,则复制字段就可以了。但是,如果对象包含对子对象的引用,则复制该字段会为您提供对该子对象的另一个引用,因此原始对象和克隆对象仍然共享一些信息。
After reading this I was wondering, that How is the clone method originally implemented in Object Class?
读完之后我想知道,克隆方法最初是如何在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?
困扰我的是:当对类中的某个方法一无所知时,Object类中的一个方法如何通过子类对象的字段克隆来创建一个字段?
11
Actually, clone()
is implemented in native code, so I assume it just does a memory copy (copy all the bytes) without knowing the contents.
实际上,clone()是用本机代码实现的,所以我假设它只是在不知道内容的情况下进行内存复制(复制所有字节)。
Besides that, there is the Reflection API to gain knowlegde about a class (which would be slower, however).
除此之外,还有Reflection API可以获得关于类的知识(然而,它会更慢)。
1
Read this from the Javadoc:
从Javadoc中读取:
protected Object clone() -
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,表达式:
x.clone() != x
x.clone()!= x
will be true, and that the expression:
将是真的,那表达式:
x.clone().getClass() == x.getClass()
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()。equals(x)将为true,但这不是绝对要求。按照惯例,返回的对象应该通过调用super.clone来获得。如果一个类及其所有超类(Object除外)都遵循这个约定,那就是这种情况
x.clone().getClass() == x.getClass().
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.
要实现此独立性,可能需要在返回之前修改super.clone返回的对象的一个或多个字段。通常,这意味着复制包含被克隆对象的内部“深层结构”的任何可变对象,并使用对副本的引用替换对这些对象的引用。如果一个类只包含原始字段或对不可变对象的引用,那么通常情况下,super.clone返回的对象中的任何字段都不需要修改。
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.
意味着当你在对象中有一个子对象时,你不应该只是克隆/复制它的引用,而是该对象的内部结构(为了创建它的新实例),如果每个对象都有它的clean clone()方法,你将能够像父对象一样克隆它,否则你将不得不创建它的新实例并逐个复制其内部premitive字段。
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2011/03/21/e77aeab71447910a48cd7c80ca787131.html。