.slice() 成为“浅克隆”意味着什么?

发布于 2024-10-19 00:47:58 字数 832 浏览 4 评论 0原文

ActionScript 的 Array 和 Vector 类都有一个 slice() 方法。如果不传递任何参数,则新的 Array 或 Vector 是原始 Vector 的副本(浅克隆)。

“浅克隆”是什么意思? 之间有什么区别

Array newArray = oldArray.slice();
Vector.<Foo> newVector = oldVector.slice();

具体来说,和

Array newArray = oldArray;
Vector.<Foo> newVector = oldVector;

?另外,如果 Vector 的基本类型不是 Foo,而是简单且不可变的类型(如 int)怎么办?

更新:

以下结果是什么?

var one:Vector.<String> = new Vector.<String>()

one.push("something");
one.push("something else");

var two:Vector.<String> = one.slice();

one.push("and another thing");

two.push("and the last thing");

trace(one); // something, something else, and another thing
trace(two); // something, something else, and the last thing

谢谢! ♥

ActionScript's Array and Vector classes both have a slice() method. If you don't pass any parameters, the new Array or Vector is a duplicate (shallow clone) of the original Vector.

What does it mean to be a "shallow clone"? Specifically, what is the difference between

Array newArray = oldArray.slice();
Vector.<Foo> newVector = oldVector.slice();

and

Array newArray = oldArray;
Vector.<Foo> newVector = oldVector;

? Also, what if the Vector's base type isn't Foo, but something simple and immutable like int?

Update:

What is the result of the following?

var one:Vector.<String> = new Vector.<String>()

one.push("something");
one.push("something else");

var two:Vector.<String> = one.slice();

one.push("and another thing");

two.push("and the last thing");

trace(one); // something, something else, and another thing
trace(two); // something, something else, and the last thing

Thanks! ♥

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

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

发布评论

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

评论(1

木緿 2024-10-26 00:47:58

在您的上下文中,.slice() 所做的只是复制矢量,以便 newArray 引用与 oldArray 不同的对象>,只不过两者看起来像是相同的物体。 newVectoroldVector 也是如此。

第二个片段:

Array newArray = oldArray;
Vector.<Foo> newVector = oldVector;

实际上使 newArray 成为 oldArray引用。这意味着两个变量引用同一个数组。 newVectoroldVector 也是如此 - 两者最终都引用相同的向量。可以将其想象为使用橡皮图章在不同的纸张上将同一个印章印两次:它是相同的印章,只是在两张纸上表示。

附带说明一下,术语浅拷贝深拷贝不同,浅拷贝是仅对象的副本,而深拷贝是对象的副本。 对象及其所有属性

此外,如果 Vector 的基本类型不是 Foo,而是像 int 这样简单且不可变的类型怎么办?

这是相同的,因为您的变量引用的是 Vector 对象,而不是它们的 int

下面的结果是什么?

您的输出是正确的:

something, something else, and another thing
something, something else, and the last thing

two = one.slice(),不带任何参数,创建 one 及其所有当前内容的新副本,并将其分配给 two。当您将第三个项目推送到 onetwo 时,您将附加到不同的 Vector 对象。

In your context, what .slice() does is simply to make a copy of your vector, so that newArray refers to a different object from oldArray, except both seem like identical objects. Likewise goes for newVector and oldVector.

The second snippet:

Array newArray = oldArray;
Vector.<Foo> newVector = oldVector;

actually makes newArray a reference to oldArray. That means both variables refer to the same array. Same for newVector and oldVector — both end up referring to the same vector. Think of it as using a rubber stamp to stamp the same seal twice on different pieces of paper: it's the same seal, just represented on two pieces of paper.

On a side note, the term shallow copy differs from deep copy in that shallow is a copy of only the object while deep is a copy of the object and all its properties.

Also, what if the Vector's base type isn't Foo, but something simple and immutable like int?

It's the same, because your variables refer to the Vector objects and not their ints.

What is the result of the following?

Your output is correct:

something, something else, and another thing
something, something else, and the last thing

two = one.slice(), without any arguments, makes a new copy of one with all its current contents and assigns it to two. When you push each third item to one and two, you're appending to distinct Vector objects.

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