从函数参数动态实例化类型化向量?

发布于 2024-10-29 02:22:13 字数 1114 浏览 2 评论 0原文

对于我正在尝试开发的游戏,我正在编写一个资源池类,以便在不调用“new”运算符的情况下回收对象。我希望能够指定池的大小,并且我希望它是强类型的。

出于这些考虑,我认为 Vector 将是我的最佳选择。但是,由于 Vector 是最终类,因此我无法扩展它。因此,我想在这种情况下我应该使用组合而不是继承。

我看到的问题是这样的 - 我想用两个参数实例化该类:大小和类类型,并且我不确定如何将类型作为参数传递。

这是我尝试过的:

public final class ObjPool
{
    private var objects:Vector.<*>;

    public function ObjPool(poolsize:uint, type:Class)
    {
        objects = new Vector.<type>(poolsize);  // line 15
    }
}

这是我尝试构建时从 FlashDevelop 收到的错误:

\src\ObjPool.as(15): col: 18 错误:访问未定义的属性类型。

有人知道有办法做到这一点吗?看起来 Flash 编译器不喜欢接受 Vector 括号表示法中的变量名称。 (我尝试将构造函数参数“type”更改为 String 作为测试,但没有结果;我还尝试将 getQualifiedClassName 放在那里,但这也不起作用。取消对象 var 的类型也没有结果。)此外,我'我什至不确定类型“Class”是否是执行此操作的正确方法 - 有人知道吗?

谢谢!

编辑:为了澄清起见,我这样称呼我的班级:

var i:ObjPool = new ObjPool(5000, int);

目的是指定大小和类型。

双重编辑:对于任何偶然发现此问题并寻求答案的人,请研究 Java 编程语言中的泛型。截至撰写本文时,它们尚未在 Actionscript 3 中实现。祝你好运。

For a game I'm attempting to develop, I am writing a resource pool class in order to recycle objects without calling the "new" operator. I would like to be able to specify the size of the pool, and I would like it to be strongly typed.

Because of these considerations, I think that a Vector would be my best choice. However, as Vector is a final class, I can't extend it. So, I figured I'd use composition instead of inheritance, in this case.

The problem I'm seeing is this - I want to instantiate the class with two arguments: size and class type, and I'm not sure how to pass a type as an argument.

Here's what I tried:

public final class ObjPool
{
    private var objects:Vector.<*>;

    public function ObjPool(poolsize:uint, type:Class)
    {
        objects = new Vector.<type>(poolsize);  // line 15
    }
}

And here's the error I receive from FlashDevelop when I try to build:

\src\ObjPool.as(15): col: 18 Error: Access of undefined property type.

Does anybody know of a way to do this? It looks like the Flash compiler doesn't like to accept variable names within the Vector bracket notation. (I tried changing constructor parameter "type" to String as a test, with no results; I also tried putting a getQualifiedClassName in there, and that didn't work either. Untyping the objects var was fruitless as well.) Additionally, I'm not even sure if type "Class" is the right way to do this - does anybody know?

Thanks!

Edit: For clarification, I am calling my class like this:

var i:ObjPool = new ObjPool(5000, int);

The intention is to specify a size and a type.

Double Edit: For anyone who stumbles upon this question looking for an answer, please research Generics in the Java programming language. As of the time of this writing, they are not implemented in Actionscript 3. Good luck.

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

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

发布评论

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

评论(5

眸中客 2024-11-05 02:22:13

我已经尝试这样做有一段时间了,多米尼克·坦克雷迪(Dominic Tancredi)的帖子让我认为即使你不能去:

objects = new Vector.<classType>(poolsize);

你也可以这样做:

public final class ObjPool
{
    private var objects:Vector.<*>;

    public function ObjPool(poolsize:uint, type:Class)
    {
        var className   : String = getQualifiedClassName(type);
        var vectorClass : Class  = Class(getDefinitionByName("Vector.<" + className + ">"));
        objects = new vectorClass(poolsize);
    }
}

我用 int 和自定义类尝试过,它似乎工作得很好。当然,您必须检查您是否真的从中获得了任何速度,因为对象是一个 Vector。<*>并且闪存可能会进行一些隐式类型检查,这会抵消使用向量获得的速度。

希望这有帮助

I have been trying to do this for a while now and Dominic Tancredi's post made me think that even if you can't go :

objects = new Vector.<classType>(poolsize);

You could go something like :

public final class ObjPool
{
    private var objects:Vector.<*>;

    public function ObjPool(poolsize:uint, type:Class)
    {
        var className   : String = getQualifiedClassName(type);
        var vectorClass : Class  = Class(getDefinitionByName("Vector.<" + className + ">"));
        objects = new vectorClass(poolsize);
    }
}

I tried it with both int and a custom class and it seems to be working fine. Of course you would have to check if you actually gain any speed from this since objects is a Vector.<*> and flash might be making some implicit type checks that would negate the speed up you get from using a vector.

Hope this helps

破晓 2024-11-05 02:22:13

这是一个有趣的问题(+1!),主要是因为我以前从未尝试过。从你的例子看来这是不可能的,我确实觉得很奇怪,可能与编译器的工作方式有关。我质疑你为什么要这样做。 Vector 相对于 Array 的性能优势主要是其类型化的结果,但是您显式地将其类型声明为未定义,这意味着您失去了性能增益。那么为什么不直接使用数组呢?不过只是食物而已。

编辑

我可以确认这是不可能的,它是一个未解决的错误。请参阅此处:http://bugs.adobe.com/jira/browse/ASC-3748< /a> 抱歉这个消息!

泰勒.

This is an interesting question (+1!), mostly because I've never tried it before. It seems like from your example it is not possible, which I do find odd, probably something to do with how the compiler works. I question why you would want to do this though. The performance benefit of a Vector over an Array is mostly the result of it being typed, however you are explicitly declaring its type as undefined, which means you've lost the performance gain. So why not just use an array instead? Just food for though.

EDIT

I can confirm this is not possible, its an open bug. See here: http://bugs.adobe.com/jira/browse/ASC-3748 Sorry for the news!

Tyler.

酒几许 2024-11-05 02:22:13

尝试远离新事物是件好事,但是:

我读过的有关 Vector<> 的所有内容都不是。在actionscript中说它必须是强类型的。所以
这不应该起作用。

编辑:我是说这是不可能的。
在这里看看这是否有帮助。

是否可以定义泛型类型Actionsctipt 3 中的向量?

It is good you trying to stay away from new but:

Everything I have ever read about Vector<> in actionscript says it must be strongly typed. So
this shouldn't work.

Edit: I am saying it can't be done.
Here see if this helps.

Is it possible to define a generic type Vector in Actionsctipt 3?

傻比既视感 2024-11-05 02:22:13

在码头拍摄,但试试这个:

var classType:Class = getDefinitionByName(type) as Class;
...
objects = new Vector.<classType>(poolsize);  // line 15

放下麦克风

Shot in the dock, but try this:

var classType:Class = getDefinitionByName(type) as Class;
...
objects = new Vector.<classType>(poolsize);  // line 15

drops the mic

浪漫人生路 2024-11-05 02:22:13

我真的不明白使用 Vector.<*> 的意义。不妨与数组一起使用。

不管怎样,我只是想出了这种动态创建向量的方法:

public function getCopy (ofVector:Object):Object
{
    var copy:Object = new ofVector.constructor;
   // Do whatever you like with the vector as long as you don't need to know the type

    return copy;
}

I don't really see the point in using a Vector.<*>. Might as well go with Array.

Anyhow, I just came up with this way of dynamically create Vectors:

public function getCopy (ofVector:Object):Object
{
    var copy:Object = new ofVector.constructor;
   // Do whatever you like with the vector as long as you don't need to know the type

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