简体   繁体   English

复制和克隆有什么区别?

[英]What is the difference between copying and cloning?

Is there a definitive reference on this in programming? 在编程中是否有明确的参考?

I see a lot of people refer to deep copying and cloning as the same thing. 我看到很多人将深度复制和克隆称为同一件事。 Is this true? 这是真的?

Is it language dependent? 它是语言依赖的吗?

A small point, but it was bothering me... 小点,但它困扰我...

There's no formal definition of these concepts, atleast not one that spans all languages. 这些概念没有正式的定义,至少没有跨越所有语言的概念。

What's usually common though: 通常是什么:

  • clone - create something new based on something that exists. 克隆 - 根据存在的东西创建新东西。
  • copying - copy from something that exists to something else (that also already exists). 复制 - 从存在的东西复制到其他东西(也已经存在)。

Yes, there is a difference. 是,有一点不同。 As far as language dependencies, some languages can do all Shallow, Deep, Lazy copying. 就语言依赖性而言,某些语言可以执行所有Shallow,Deep,Lazy复制。 Some only do Shallow copies. 有些只做浅版。 So yes, it is language dependent sometimes. 所以是的,它有时依赖于语言。

Now, take for instance an Array: 现在,以一个数组为例:

int [] numbers = { 2, 3, 4, 5};
int [] numbersCopy = numbers;

The “numbersCopy” array now contains the same values, but more importantly the array object itself points to the same object reference as the “numbers” array. “numbersCopy”数组现在包含相同的值,但更重要的是,数组对象本身指向与“数字”数组相同的对象引用。

So if I were to do something like: 所以,如果我做的事情如下:

  numbersCopy[2] = 0;

What would be the output for the following statements? 以下陈述的输出结果是什么?

  System.out.println(numbers[2]);

  System.out.println(numbersCopy[2]);

Considering both arrays point to the same reference we would get: 考虑到两个数组都指向相同的引用,我们会得到:

0 0

0 0

But what if we want to make a distinct copy of the first array with its own reference? 但是,如果我们想要使用自己的引用制作第一个数组的独特副本呢? Well in that case we would want to clone the array. 那么在这种情况下我们想要克隆数组。 In doing so each array will now have its own object reference. 这样做,每个数组现在都有自己的对象引用。 Let's see how that will work. 让我们看看它将如何运作。

  int [] numbers = { 2, 3, 4, 5};

  int [] numbersClone = (int[])numbers.clone();

The “numbersClone” array now contains the same values, but in this case the array object itself points a different reference than the “numbers” array. “numbersClone”数组现在包含相同的值,但在这种情况下,数组对象本身指向的参考不同于“数字”数组。

So if I were to do something like: 所以,如果我做的事情如下:

  numbersClone[2] = 0;

What would be the output now for the following statements? 现在以下陈述的输出是什么?

  System.out.println(numbers[2]);

  System.out.println(numbersClone[2]);

You guessed it: 你猜到了:

4 4

0 0

Source 资源

Most concise: 最简洁:

  • copy: replicate to existing instance (shallow or deep) copy:复制到现有实例(浅或深)
  • clone: replicate to new instance (always deep) clone:复制到新实例(总是很深)

No consensus as developers sloppily interchange them; 没有达成共识,因为开发人员勉强互换它们 however one could lobby the above based on: 然而,人们可以根据以下内容游说

  1. Etymology (Biology) implies that the notion of a "shallow clone" is nonsensical since not genetically identical; 词源(生物学)暗示“浅克隆”的概念是荒谬的,因为它不是基因相同的; cloning implies completeness in order to propagate the entity. 克隆意味着完整性以传播实体。
  2. Copying historically implies replication onto existing medium (copying a book or painting, etc.) Eg, a photocopy copies an image onto an existing piece of paper; 历史复制意味着复制到现有媒体上(复制书籍或绘画等)。例如,复印件将图像复制到现有纸张上; if one could somehow clone a piece of paper the result would be a new piece of paper. 如果有人能以某种方式克隆一张纸,结果将是一张新纸。
  3. One could "copy" an object reference but one would never "clone" an object reference. 可以“复制”对象引用,但永远不会“克隆”对象引用。

I would say that copy and cloning are analogous terms. 我会说复制和克隆是类似的术语。 The only thing that you should maybe be aware is that you get shallow copy and deep copy. 你唯一应该注意的是你得到浅拷贝和深拷贝。 Shallow copy only makes a copy of an object at the root level where as deep copy will produce a copy of an object and all its child objects. 浅拷贝仅在根级别创建对象的副本,而深拷贝将生成对象及其所有子对象的副本。

In C++-land "cloning" is usually idiom for deep copying polymorphic classes' objects. 在C ++中 - 土地“克隆”通常是深度复制多态类对象的习语。

In Java/C# I suspect these terms used more interchangeably. 在Java / C#中,我怀疑这些术语可以互换使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM