English 中文(简体)
ActionScript 矢量铸造不一致吗?
原标题:ActionScript Vector casting inconsistency?

当矢量被像这样投射的时候...

var v1:Vector.<String> = new Vector.<String>();
v1.push("foo");

var v2:Vector.<Object> = Vector.<Object>(v1)

v1.push("bar");

trace(v1);   //foo,bar
trace(v2);  //foo

... 矢量的复制件被创建, 正如您在微量输出中看到的 。

但是当你把第3行改成...

var v2:Vector.<*> = Vector.<*>(v1)

...... 没有创建副本, v1 和 v2 都指向同一个对象, 微量输出将同时为“ foo, bar ” 。

为什么 难道不该有一贯的行为吗

最佳回答

您必须意识到, Vector. < somethings> (Vector.< Anotherthing>) 不是一个投影类型。 事实上, 您不能将一种矢量投放到另一种矢量的矢量上 。 请尝试一下 :

var v1:Vector.<String> = new Vector.<String>();
var v2:Vector.<Object> = v1 as Vector.<Object>; // throws an error

您也不能指定子类型矢量 :

var v1:Vector.<Object> = new Vector.<String>(); // throws an error

这是因为当字符串是对象的子类型时,两种矢量是与 无关。

那么为什么您的标记没有失败? 因为您正在真正做的是调用 < a href=" http://help. adobe.com/en_US/FlashPlatform/ reference/ actionscript/3/package. html# Vector% 28% 29" rel=“ nofollow”> 顶层函数 Vector () , 它从您通过的任何收藏类型数据创建矢量。 文档上写着 :

如果源阵列参数已经是一个 Vector.<T> 实例, 其中T 是基准类型, 函数返回矢量。 否则, 结果矢量将包含源阵列或矢量的元素 。

您类型声明中的星号是一个占位符, 因此变量将持有任何输入的矢量实例, 并允许函数返回您通过相同的矢量 。

问题回答

暂无回答




相关问题
Attaching a property to an event in Flex/AS3

I have a parameter that needs to be passed along with an event. After unsuccessful attempts to place it on the type by extending the class, I ve been advised in another SO question to write a custom ...

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

What s a good way of deserializing data into mock objects?

I m writing a mock backend service for my flex application. Because I will likely need to add/edit/modify the mock data over time, I d prefer not to generate the data in code like this: var mockData =...

AS3 try/catch out of memory

I m loading a few huge images on my flex/as3 app, but I can t manage to catch the error when the flash player runs out of memory. Here is the what I was thinking might work (I use ???? because i dont ...

热门标签