动态实例化的函数参数类型化的载体? [英] Dynamically instantiate a typed Vector from function argument?

查看:189
本文介绍了动态实例化的函数参数类型化的载体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于比赛,我试图发展,我写一个资源池类以回收对象,而不调用新运营商。我想可以指定池的大小,我希望它是强类型。

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.

由于这些考虑,我认为,一个Vector将是我最好的选择。然而,由于矢量是一个final类,我不能扩展。所以,我想我会使用,而不是继承组成,在这种情况下。

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.

下面是我的尝试:

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

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

下面是我从FlashDevelop中收到错误,当我尝试建立:

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

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

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

有谁知道的方式做到这一点?它看起来像Flash编译器不喜欢接受矢量括号表示法中的变量名。 (我试图改变构造函数参数类型为字符串作为测试,没有结果,我也试图把一个的getQualifiedClassName在那里,而且没有任何工作Untyping对象VAR是无果而终,以及。)另外,我中号甚至不知道类型的类是正确的方式做到这一点? - 有谁知道

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?

谢谢!

编辑:为了澄清,我打电话给我的阶级是这样的:

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.

双击编辑:对于任何在这个问题寻找答案谁绊倒,请研究的 Java编程语言泛型。在编写这本书的时候,他们在ActionScript实现3好运。

Double 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.

推荐答案

这是一个有趣的问题(+1!),这主要是因为我以前从来没有尝试过。这似乎是从你的榜样是不可能的,这一点我觉得奇怪,也许是与编译器是如何工作的。我问你为什么会想这样做,虽然。一个向量在一个阵列的性能好处主要是它的结果是类型化的,但是你明确地声明其类型为不确定的,这意味着你已经失去了性能增益。那么,为什么不直接使用数组呢?只是食物虽然。

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.

修改

我可以证实,这是不可能的,它的开放式问题。在这里看到: http://bugs.adobe.com/jira/browse/ASC-3748 很抱歉的消息!

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!

泰勒。

这篇关于动态实例化的函数参数类型化的载体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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