如何获取变量类型?并实例化它? [英] How to get type of variable? and instantiate it?
问题描述
我正在尝试创建一个函数来打开一个窗口,但要确保同一个窗口尚未打开.我希望能够将一个非实例化的 var 或一个实例化的 var 传递给它,它可以以任何一种方式工作.如果窗口已经打开,则关闭它然后重新打开它.
I am trying to make a function that opens a window but makes sure the same window is not already open. I want to be able to pass it a non-instantiated var or an instantiated var and it work either way. If the window is already open it closes it then reopens it.
所以我需要一种方法来传递一个 Window 类型的变量或一个子类(如果有),并实例化正确的子类.
So I need a way to pass a variable of type Window or a subclass if it, and instantiate the proper subclass.
我正在寻找这样的东西:
I am looking for something like this:
public function openWindowOnce(window:Window):void
{
if(isOpen(window))
{
closeIfOpen(window);
}
window = new Window(); /**<-- THIS LINE window can also be a sublcass of window,
* I want to instatiate the correct sublass,
* I also want to make sure that it is a Window or a
* Sublcass of window before I instatiate it.
*/
window.open();
}
谢谢!
推荐答案
您可以尝试组合使用 flash.utils.getDefinitionByName()
, flash.utils.getQualifiedClassName()
和 ClassFactory
来实现结果.
You can try using a combination of flash.utils.getDefinitionByName()
, flash.utils.getQualifiedClassName()
and ClassFactory
to achieve the result.
var className:string = getQualifiedClassName(object); //returns the class name
var classObj:Class = getDefinitionByName(className) as Class; //get a Class object
var factory:IFactory = new ClassFactory(classObj);// get a Class factory
var newObj:Object = factory.newInstance();
这篇关于如何获取变量类型?并实例化它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!