`
darrenzhu
  • 浏览: 802498 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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

阅读更多
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与JavaScript交互实例

    在JavaScript中,`addMethod`函数通过`JavaScriptConnectFlex.addTo100()`来调用Flex中的`add`函数,`JavaScriptConnectFlex`是Flex应用程序的全局对象,`addTo100`是之前在Flex中注册的回调函数名。这使得...

    Flex学习—关于Shareobject对象(本地共享对象)

    2. 数据操作:创建实例后,你可以像操作普通对象一样,通过属性或方法来读写数据。例如: ```actionscript so.data.myProperty = "Some value"; ``` 3. 监听事件:为了追踪数据的变化,可以监听ShareObject的事件,...

    flex打印实例源码

    3. **设置打印属性**:通过`PrintJob`对象的属性,如`jobName`、`showPrintOptions`等,可以定制打印作业的名称和是否显示打印选项对话框。 4. **启动打印**:调用`start()`方法启动打印作业。用户可以选择打印机、...

    Flex创建流程图的实例

    我们需要创建一个自定义的类来表示这些对象,包含必要的属性如ID、名称、位置等。 3. **实现GraphDataModel** - 创建一个GraphDataModel实例,它将作为流程图的数据源。在此模型中,我们添加节点和边的数据结构,...

    TWaver 3D Flex 3D 实例 例子

    从标签“Flex 3D 实例 例子”中我们可以推测,内容将专注于编程实践,包括3D场景的创建、3D对象的操纵、用户交互以及可能的数据绑定等。 根据压缩包内的文件名称列表,我们有以下组件: 1. **License.txt**:通常...

    flex 后台管理系统

    4. Flex组件:在提供的文件名称列表中,我们看到了如MyApp.mxml、Edit.mxml、Cart.mxml等文件,这些都是Flex应用中的组件或模块。MXML是Flex中用于构建用户界面的标记语言,类似于HTML。这些文件可能分别代表应用的...

    Flex编译错误码总结

    确保在调用对象的方法或访问其属性之前,该对象已经被正确实例化。 2. Error 1046: Type was not found or was not a compile-time constant: className 这表示编译器无法找到指定的类名或该类在编译时未定义。检查...

    上一篇的flex的远程对象调用,flex和spring集成分页的jar和截图

    通过RO服务,Flex应用可以与服务器端的业务逻辑进行交互,通常涉及AMF(Action Message Format)数据序列化,它允许高效地传输复杂的数据结构。 描述中提到的“flex和spring集成分页”是指将Flex客户端与Spring框架...

    flex DataGrid xml 动态数据列表实例

    2. **加载XML数据**:在Flex应用中,我们可以使用`XML`类加载并解析XML文件。例如: ```actionscript var urlLoader:URLLoader = new URLLoader(); urlLoader.dataFormat = URLLoaderDataFormat.TEXT; urlLoader....

    Flex基础培训

    4. **使用自定义事件类**:在MXML文件中定义自定义事件,并在ActionScript代码中实例化该事件类的对象,设置数据后派发。 ```xml [Event(name="searchEvent", type="events.SearchEvent")] &lt;![CDATA[ ...

    将 Flex 集成到 Java EE 应用程序的最佳实践(完整源代码)

    FlexFactoryImpl 负责实例化 SpringFactoryInstance 并通过 SpringFactoryInstance 的 lookup() 方法查找 FlexService 接口对象: 清单 10. FlexFactoryImpl class public class FlexFactoryImpl implements...

    Flex Spring JAVA BLAZEDS整合

    但通过`SpringRemotingDestinationBootstrapService`,我们可以自动导出带有@Service注解且名称以“FlexService”结尾的Spring Bean,将其转换为BlazeDS的RemoteObject。 3. **配置属性**: `...

    +Flex+集成到+Java+EE+应用程序的最佳实践(完整源代码)

    FlexFactoryImpl 负责实例化 SpringFactoryInstance 并通过 SpringFactoryInstance 的 lookup() 方法查找 FlexService 接口对象: 清单 10. FlexFactoryImpl class public class FlexFactoryImpl implements ...

    flex中actionscript动态调用remoteobject类

    1. **RemoteObject**:Flex中的RemoteObject类是用于远程调用服务器端服务的对象。它可以调用后端提供的服务,并接收返回的结果。 2. **AMFChannel**:AMFChannel是Flex中用于发送和接收数据的通道之一。它支持AMF...

    flex 打包解压缩

    创建一个新的ZIP文件,你可以通过实例化`ZipArchive`对象并调用其`addFile()`方法来添加文件。例如: ```actionscript var zip:ZipArchive = new ZipArchive(); zip.addFile("path/to/your/file", "entryName"); ``...

    flex实现边框

    这个方法在组件的背景被绘制时调用,可以通过`Graphics`对象来绘制线条、形状等。例如: ```actionscript override protected function drawBackground(graphics:Graphics):void { graphics.beginFill(0xCCCCCC);...

    Flex事件机制(通俗易懂)

    在Flex中,事件的分发是通过继承自 `EventDispatcher` 类的对象的 `dispatchEvent` 方法来完成的。这个方法接受一个事件对象作为参数,并将其广播出去。其他监听该事件的组件可以通过事件的名称来判断是否接收这个...

    flex+java前后台通讯

    1. 实例化RemoteObject:`service = new RemoteObject("helloService");` 这里"helloService"是服务的标识,对应Java端的服务接口或类。 2. 设置请求超时时间:`service.requestTimeout = 600;` 这意味着如果在10...

    flex 打开指定类型的打开对话框

    - **实例化**:创建`FileReference`对象。 - **browse() 方法**:弹出文件选择对话框。 - **upload() 方法**:上传文件到服务器。 #### 2.2 FileReferenceList - **实例化**:创建`FileReferenceList`对象。 - **...

Global site tag (gtag.js) - Google Analytics