我注意到int
,uint
和Number
类型的向量出现了一些奇怪的行为。这是您可以自己测试的一些代码:
package
{
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
private var untypeds:Vector.<*> = new <*>[{}, 7];
private var objects:Vector.<Object> = new <Object>[{}, {}];
private var sprites:Vector.<Sprite> = new <Sprite>[new Sprite(), new Sprite()];
private var events:Vector.<Event> = new <Event>[new Event("1"), new Event("2")];
private var booleans:Vector.<Boolean> = new <Boolean>[true, false];
private var strings:Vector.<String> = new <String>["1", "2"];
private var ints:Vector.<int> = new <int>[-5, -6];
private var uints:Vector.<uint> = new <uint>[5, 6];
private var numbers:Vector.<Number> = new <Number>[2.5, 5.2];
public function Main()
{
myFunc("untypeds");
myFunc("objects");
myFunc("sprites");
myFunc("events");
myFunc("booleans");
myFunc("strings");
myFunc("ints");
myFunc("uints");
myFunc("numbers");
}
// Note that I'm only using a string parameter for tracing purposes.
// The same results would be seen if the vector was passed directly as an untyped argument.
private function myFunc(vecName:String):void
{
var vec:Vector.<*> = this[vecName] as Vector.<*>;
trace(vecName + " as Vector.<*>: " + vec + " Same object? " + (vec == this[vecName]));
vec = Vector.<*>(this[vecName]);
trace("Vector.<*>(" + vecName + "): " + vec + " Same object? " + (vec == this[vecName]));
}
}
}
这是输出:
untypeds as Vector.<*>: [object Object],7 Same object? true
Vector.<*>(untypeds): [object Object],7 Same object? true
objects as Vector.<*>: [object Object],[object Object] Same object? true
Vector.<*>(objects): [object Object],[object Object] Same object? true
sprites as Vector.<*>: [object Sprite],[object Sprite] Same object? true
Vector.<*>(sprites): [object Sprite],[object Sprite] Same object? true
events as Vector.<*>: [Event type="1" bubbles=false cancelable=false eventPhase=2],[Event type="2" bubbles=false cancelable=false eventPhase=2] Same object? true
Vector.<*>(events): [Event type="1" bubbles=false cancelable=false eventPhase=2],[Event type="2" bubbles=false cancelable=false eventPhase=2] Same object? true
booleans as Vector.<*>: true,false Same object? true
Vector.<*>(booleans): true,false Same object? true
strings as Vector.<*>: 1,2 Same object? true
Vector.<*>(strings): 1,2 Same object? true
ints as Vector.<*>: null Same object? false
Vector.<*>(ints): -5,-6 Same object? false
uints as Vector.<*>: null Same object? false
Vector.<*>(uints): 5,6 Same object? false
numbers as Vector.<*>: null Same object? false
Vector.<*>(numbers): 2.5,5.2 Same object? false
请注意,转换为Vector。&lt; *&gt;除int
,uint
和Number
外,在任何情况下均可正常使用。在这些情况下,使用全局Vector()
函数进行投射会返回向量的副本,并使用as
进行投射会返回null
。
有没有办法将数字向量传递给一个旨在修改任何类型的向量的函数?
编辑:我在尝试将Vector.<int>
强制转换为Vector.<Number>
时遇到了同样的问题,因此为这三种数字类型添加一个额外版本的函数将无效。
答案 0 :(得分:1)
“有没有办法将数字向量传递给函数来修改任何类型的向量?”
不知道这是否是你想要的,只是使用通配符(*
)作为函数的输入参数。这将允许您使用不同的数字类型向量。
var ints:Vector.<int> = new Vector.<int>();
var numbers:Vector.<Number> = new Vector.<Number>();
ints.push( 5, 3, 7 );
numbers.push( -2.8, 1.5, -1.4 );
function vect_Push (input:*) : void
{
input.push( input[input.length-1] * 2 );
}
function vect_Mixed (inputA:* , inputB:*) : void
{
inputB[inputB.length-1] = inputA[inputA.length-1] * inputB[inputB.length-1];
}
在上面的代码中......
function vect_Push
将采用任何vector
或array
(实际上任何响应.push
方法的数据类型)并再添加一个新条目。function vect_Mixed
将采用两个混合的vector
数组(因此inputA
可以是ints
,而inputB
可以是numbers
。最后一个条目一个将与其他一个相乘)。测试上面的代码......
//# test for : modify any vector (using Push to add one extra new entry)
trace("Old length ints : " + ints.length);
vect_Push(ints); //passing Integer to function
trace("New length ints : " + ints.length); //should be +1 length
trace("Last new value ints : " + ints[ints.length-1]); //check new entry's value
//# test for : mixed vector types (multply types "int" with "Number")
vect_Mixed(ints, numbers); //passing Integer and Number to function
trace("New mult value numbers[last entry] : " + numbers[numbers.length-1]);