是否可以在 Actionsctipt 3 中定义泛型类型 Vector? [英] Is it possible to define a generic type Vector in Actionsctipt 3?

查看:19
本文介绍了是否可以在 Actionsctipt 3 中定义泛型类型 Vector?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要制作一个 VectorIterator,所以我需要接受任何类型的 Vector.我目前正在尝试将类型定义为 *,如下所示:

Hi i need to make a VectorIterator, so i need to accept a Vector with any type. I am currently trying to define the type as * like so:

var collection:Vector.<*> = new Vector<*>()

但是编译器抱怨类型不是编译时常量".我知道 Vector 类存在错误报告,报告错误类型为缺失,例如:

But the compiler is complaining that the type "is not a compile time constant". i know a bug exists with the Vector class where the error reporting, reports the wrong type as missing, for example:

var collection:Vector.<Sprite> = new Vector.<Sprite>()

如果 Sprite 没有被导入,编译器会抱怨它找不到 Vector 类.请问这有关系吗?

if Sprite was not imported, the compiler would complain that it cannot find the Vector class. I wonder if this is related?

推荐答案

所以看起来答案是没有办法将类型的 Vector 隐式转换为有效的超类型.它必须使用全局 Vector.<> 函数显式执行.

So it looks like the answer is there is no way to implicitly cast a Vector of a type to valid super type. It must be performed explicitly with the global Vector.<> function.

所以我的实际问题是混合问题:)

So my actual problem was a mix of problems :)

使用 Vector 是正确的.作为对另一个 Vector 的通用引用,但不能像这样执行:

It is correct to use Vector. as a generic reference to another Vector, but, it cannot be performed like this:

var spriteList:Vector.<Sprite> = new Vector.<Sprite>()
var genericList:Vector.<Object> = new Vector.<Object>()
genericList = spriteList // this will cause a type casting error

应该使用全局 Vector() 函数/强制转换来执行赋值,如下所示:

The assignment should be performed using the global Vector() function/cast like so:

var spriteList:Vector.<Sprite> = new Vector.<Sprite>()
var genericList:Vector.<Object> = new Vector.<Object>()
genericList = Vector.<Object>(spriteList)

这是一个简单的案例,我没有阅读文档.

It was a simple case of me not reading the documentation.

下面是一些测试代码,我希望是 Vector.隐式转换为 Vector.<*>.

Below is some test code, I would expect the Vector. to cast implicitly to Vector.<*>.

public class VectorTest extends Sprite
{
    public function VectorTest()
    {
        // works, due to <*> being strictly the same type as the collection in VectorContainer
        var collection:Vector.<*> = new Vector.<String>() 

        // compiler complains about implicit conversion of <String> to <*>
        var collection:Vector.<String> = new Vector.<String>()
        collection.push("One")
        collection.push("Two")
        collection.push("Three")

        for each (var eachNumber:String in collection)
        {
            trace("eachNumber: " + eachNumber)
        }

        var vectorContainer:VectorContainer = new VectorContainer(collection)

        while(vectorContainer.hasNext())
        {
            trace(vectorContainer.next) 
        }
    }
}

public class VectorContainer
{
    private var _collection:Vector.<*>

    private var _index:int = 0

    public function VectorContainer(collection:Vector.<*>)
    {
        _collection = collection
    }

    public function hasNext():Boolean
    {
        return _index < _collection.length
    }

    public function get next():*
    {
        return _collection[_index++]
    }
}

这篇关于是否可以在 Actionsctipt 3 中定义泛型类型 Vector?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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