.slice() 成为“浅克隆”意味着什么?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的上下文中,
.slice()
所做的只是复制矢量,以便newArray
引用与oldArray
不同的对象>,只不过两者看起来像是相同的物体。newVector
和oldVector
也是如此。第二个片段:
实际上使
newArray
成为oldArray
的引用。这意味着两个变量引用同一个数组。newVector
和oldVector
也是如此 - 两者最终都引用相同的向量。可以将其想象为使用橡皮图章在不同的纸张上将同一个印章印两次:它是相同的印章,只是在两张纸上表示。附带说明一下,术语浅拷贝与深拷贝不同,浅拷贝是仅对象的副本,而深拷贝是对象的副本。 对象及其所有属性。
这是相同的,因为您的变量引用的是
Vector
对象,而不是它们的int
。您的输出是正确的:
two = one.slice()
,不带任何参数,创建one
及其所有当前内容的新副本,并将其分配给two
。当您将第三个项目推送到one
和two
时,您将附加到不同的Vector
对象。In your context, what
.slice()
does is simply to make a copy of your vector, so thatnewArray
refers to a different object fromoldArray
, except both seem like identical objects. Likewise goes fornewVector
andoldVector
.The second snippet:
actually makes
newArray
a reference tooldArray
. That means both variables refer to the same array. Same fornewVector
andoldVector
— 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.
It's the same, because your variables refer to the
Vector
objects and not theirint
s.Your output is correct:
two = one.slice()
, without any arguments, makes a new copy ofone
with all its current contents and assigns it totwo
. When you push each third item toone
andtwo
, you're appending to distinctVector
objects.