如何获得变量的类型?并创建实例? [英] How to get type of variable? and instantiate it?

查看:133
本文介绍了如何获得变量的类型?并创建实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让,打开一个窗口,但可以确保在同一窗口尚未打开的功能。我希望能够通过它是非实例化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.

所以我需要一个方式来传递式窗口的变量或子类,如果它和实例化适当的子类。

So I need a way to pass a variable of type Window or a subclass if it, and instantiate the proper subclass.

我要寻找的是这样的:

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屋!

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