当前位置:首页 > 开发 > Web前端 > 前端 > 正文

flex如何通过类名称实例化对象

发表于: 2012-11-30   作者:darrenzhu   来源:转载   浏览次数:
摘要: Getting the class from an object Class(getDefinitionByName(getQualifiedClassName(obj))); Getting Class info of an Object in Flex Frequently we may come across one requirement when we will be ha
Getting the class from an object
Class(getDefinitionByName(getQualifiedClassName(obj)));

Getting Class info of an Object in Flex
Frequently we may come across one requirement when we will be having one object and we want to know which class this object belongs to. In java, its as simple as object. getClass();
Flex also provides us with a class, mx.utils.ObjectUtil, which we can use for this purpose.
ObjectUtil.getClassInfo(object)
will return information about the class, and properties of the class, for the specified Object.
• name: String containing the name of the class;
• Properties: Sorted list of the property names of the specified object.
These are the properties we can use for that.
Alert.show(ObjectUtil.getClassInfo(faultEvent.target).name);
It’s often useful to know which class had generated some fault event and we can use the above code

How to instantiate Class from Class Name
One frequently asked question is how to instantiate a Class if you don't know the Class until runtime. The "new" operator simply operates on a Class. There's no reason that class has to be hard coded. It could be a variable of Class type. The trick then becomes setting the variable. Often this is done by using getDefintionByName () as below:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" applicationComplete="init()">
  <mx:Script>
     <![CDATA[
        import flash.utils.getDefinitionByName;
        import myPackage.mySubPackage.MyClass;  // all classes you may want to instantiate should be imported.
        import myPackage.mySubPackage.MyClass2;  // all classes you may want to instantiate should be imported.
    
        public var dummy:MyClass;  // forces "MyClass" to be linked in by the complier";
        public var dummy:MyClass2;  // forces "MyClass2" to be linked in by the complier";

        public function init():void {
           var className:String;
           if (true) { // really this should be a useful conditional
              className = "myPackage.mySubPackage.MyClass";  //use fully qualified name
           } else {
              className = "myPackage.mySubPackage.MyClass2";  //use fully qualified name
           }
           var definition:Class = getDefinitionByName(className) as Class; // get class
           var myInstance:Object = new definition();  // create new instance of the class of type MyClass
        }
      ]]>
</mx:Script>
</mx:Application>



However this requires you to have the fully qualified name. Sometimes this isn't practical. You could thus do something simpler which requires a little more hard coding:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" applicationComplete="init()">
  <mx:Script>
     <![CDATA[
        import flash.utils.getDefinitionByName;
        import myPackage.mySubPackage.MyClass;
        import myPackage.mySubPackage.MyClass2;

        public function init():void {
           var definition:Class;
           if (true) { // really this should be a useful conditional
              definition = myClass;
           } else {
              definition = myClass2
           }
           var myInstance:Object = new definition();  // create new instance of the class of type 
MyClass            }
      ]]>
</mx:Script>
</mx:Application>


Lastly, any classes that are dynamically instantiated have to have been compiled into the SWF (or retrieved from a module/RSL/etc). Simply importing a Class does not accomplish this. (Importing is simply a way so you don't have to write the package name in front of the class every time.) There are three main ways (that I know of) to link a Class into a project.

The first is the method is to use the Class somewhere in the code. Examples of this include (as above) defining a variable of the class type or assigning the Class to a variable of type Class. Another example is rschmidt's comment below that you can instantiate an array that holds the classes.

The second method is to use the extraClass property of the Frame or Mixin tag. Warning, these methods may cause linkage problems.

Lastly, you can use the compiler options (how boring).

Personally, I think I prefer rschmidt's method. It's the most economical with code (and probably other resources as well).

flex如何通过类名称实例化对象

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
private Control FindControl(Control control, string controlName) { Control c1; foreach (Contr
上图展示的是类生命周期流向;在本文里,我只打算谈谈类的"初始化"以及"对象实例化"两个阶段。 类"
在上章中讲解了类文件的生成和包含的信息,但是要加载到jvm中才能启动和运行; #、一直以为 初始化
之前在项目中碰到这样一个问题:   类B继承了类A,B在实例化的时候,A的构造方法中调用了B的某个
  在开始正文之前,我先来区分一下标题的这两个词的含义。所谓类型对象,就是指运行时为加载到应
类实例即对象。对象的初始化过程也就是类的实例化过程。 inside JVM作者Bill Venners关于对象初始化
类实例即对象。对象的初始化过程也就是类的实例化过程。 inside JVM作者Bill Venners关于对象初始化
类实例即对象。对象的初始化过程也就是类的实例化过程。 inside JVM作者Bill Venners关于对象初始化
自ActionScript 3开始,eval函数就被取消了,这样就不能像原来那样利用字符串动态的创建对象了,但
自ActionScript 3开始,eval函数就被取消了,这样就不能像原来那样利用字符串动态的创建对象了,但
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号