AS3:将变量强制转换为动态类型

编程入门 行业动态 更新时间:2024-10-25 18:30:26
本文介绍了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:将变量强制转换为动态类型

本文发布于:2023-10-14 21:44:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1492301.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   变量   类型   动态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!