AS3:将变量强制转换为动态类型 [英] AS3: Cast Variable as Dynamic Type

查看:114
本文介绍了AS3:将变量强制转换为动态类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将变量动态地转换为类型,而该类型将是类型为Class的变量?

Is it possible to cast a variable as a type dynamically, where the type would be a variable of type Class?

请考虑以下内容(无效):

Consider the following (invalid):

var myClass:Class = MyClass;
var myInstance:myClass = new myClass();

对于上下文:我正在Flex(4)中创建一个模式管理器,该模式管理器将控制创建模式的各个方面(通过PopUpManager),我希望将其保持在最小限度.组件将通过相同的函数传递,而不是允许任何类型的变量进行强制转换,例如:

For context: I'm working in Flex (4) to create a modal manager that will control various aspects of creating modals (via PopUpManager) and I'd like to keep it as minimal as possible. Components would be passed through the same functions, and rather than allowing any type of variable to be cast, eg:

var myInstance:* = new myClass();

我宁愿保持尽可能明确.授予AFAIK这样做没有真正的优势,因为在分配之前会进行检查以确保参数的类型为Class-它比任何东西都更像POC.

I'd rather keep it as explicit as possible. Granted AFAIK there is no -real- advantage to doing this, as there are checks to make sure that the argument is of type Class before assigning - it's more of a POC than anything.

欢迎任何想法.

我想我可以做以下事情:

I suppose I could do the following:

var myClass:Class = MyClass;
var myInstance:* = new myClass() as myClass;

但是我不确定这是否是最好的解决方法.

but I'm not sure if that's the best way to go about it.

推荐答案

我认为编译器无法处理动态类型.如果可能的话,将有可能实现类似于泛型的功能.

I don't think the compiler can handle dynamic typing. If it could, it would be possible to implement features similar to Generics.

我认为您能做的最好的事情是:

I think the best you can do is:

var myClass:Class = MyClass;
var myObject:* = new myClass();

这篇关于AS3:将变量强制转换为动态类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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