C# 复制和克隆有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2200409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:20:02  来源:igfitidea点击:

What is the difference between copying and cloning?

c#oop

提问by LeWoody

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...

一个小问题,但它困扰着我......

采纳答案by David

Yes, there is a difference. As far as language dependencies, some languages can do all Shallow, Deep, Lazy copying. 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”数组现在包含相同的值,但更重要的是数组对象本身指向与“numbers”数组相同的对象引用。

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”数组现在包含相同的值,但在这种情况下,数组对象本身指向与“numbers”数组不同的引用。

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

来源

回答by Alexander Poluektov

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# 中,我怀疑这些术语可以互换使用。

回答by David

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.

我会说复制和克隆是类似的术语。您可能唯一应该注意的是,您会获得浅拷贝和深拷贝。浅拷贝仅在根级别创建对象的副本,而深拷贝将生成对象及其所有子对象的副本。

回答by Anonym

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).
  • 克隆 - 基于现有的东西创建新的东西。
  • 复制 - 从存在的东西复制到其他东西(也已经存在)。

回答by Pete Alvin

Most concise:

最简洁:

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

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.) E.g., 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.
  1. 词源学(生物学)暗示“浅层克隆”的概念是荒谬的,因为在基因上不相同;克隆意味着为了传播实体的完整性。
  2. 历史上的复制意味着复制到现有媒体上(复制一本书或绘画等)。如果有人能以某种方式克隆一张纸,结果将是一张新纸。
  3. 人们可以“复制”一个对象引用,但永远不会“克隆”一个对象引用。