clone()对象是什么意思? [英] What does it mean to clone() an object?

查看:90
本文介绍了clone()对象是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是vb6或Java中的对象克隆?在什么情况下我们使用克隆?克隆对象是什么意思?

What is object cloning in vb6 or java? In what situation do we use a clone? What does cloning objects mean? Can any one tell me with example please.

推荐答案

克隆实际上是将对象数据复制到新对象中。

Cloning is actually copying the object data into a new object.

此示例未克隆数据:

Foo p = new Foo();
Foo o = p;

如果 Foo 具有成员 a 并且您更改了 pa ,然后 oa 也会更改,因为两个 p o 指向同一对象。

If Foo has a member a and you change p.a then o.a also changes because both p and o point to the same object.

但是,

Foo p = new Foo();
Foo o = p.Clone();

在这种情况下,如果更改 pa oa 保持不变,因为它们实际上指向单独的对象。

In this case if you change p.a then o.a remains the same because they actually point to separate objects.

实际上,有两种克隆方法:浅克隆或深克隆。

There are actually two different ways you can clone: shallow clone or deep clone.

浅克隆只是创建一个新对象并将成员复制到新对象中。这意味着,如果成员之一实际上是指向另一个对象的指针,则该对象将在旧对象和新对象之间共享。

A shallow clone just makes a new object and copies the members into the new object. This means that if one of the members is actually a pointer to another object then that object will be shared between the old object and new object.

深层克隆实际上会通过并将所有成员克隆到新对象中。这样,对象就是所有数据的完整副本。

A deep clone actually goes through and clones all the members into the new object. That way the objects are complete copies of all the data.

这篇关于clone()对象是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆