Flex 反射

    技术2025-08-29  9

    <?xml version="1.0" encoding="utf-8"?>   <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">       <mx:Script>           <![CDATA[               import reflect.ReflectTest;               import mx.controls.Alert;               import flash.utils.getDefinitionByName;                              private function test():void{                   mx.controls.Alert.show("flex 反射");               }               private function test2():void{                                      var tt:Function=this.test;                   tt.apply(this,null);                                      tt.call(this);               }                                             //反射调用方法               private function test3():void{                   var cla:Class=getDefinitionByName("reflect.ReflectTest") as Class;                   var instance:Object=new cla();                   if(instance.hasOwnProperty("flex")){                       var fun:Function=instance["flex"];                       fun.apply(this);                       Alert.show("调用成功 哈哈");                   }                                  }                          private function test4():void{                   var refle:ReflectTest=new ReflectTest;                   var fun:Function=refle.flex;                   fun.call(this);//不知道this属性是干什么用的                                  }               private function test5():void{                   var refle:ReflectTest=new ReflectTest;                   refle["flex"].apply();//网上一直有这种写法,老认为不可能,也没去试               }           ]]>       </mx:Script>              <mx:Button click="test3()">                  </mx:Button>   </mx:Application>   <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> <mx:Script> <![CDATA[ import reflect.ReflectTest; import mx.controls.Alert; import flash.utils.getDefinitionByName; private function test():void{ mx.controls.Alert.show("flex 反射"); } private function test2():void{ var tt:Function=this.test; tt.apply(this,null); tt.call(this); } //反射调用方法 private function test3():void{ var cla:Class=getDefinitionByName("reflect.ReflectTest") as Class; var instance:Object=new cla(); if(instance.hasOwnProperty("flex")){ var fun:Function=instance["flex"]; fun.apply(this); Alert.show("调用成功 哈哈"); } } private function test4():void{ var refle:ReflectTest=new ReflectTest; var fun:Function=refle.flex; fun.call(this);//不知道this属性是干什么用的 } private function test5():void{ var refle:ReflectTest=new ReflectTest; refle["flex"].apply();//网上一直有这种写法,老认为不可能,也没去试 } ]]> </mx:Script> <mx:Button click="test3()"> </mx:Button> </mx:Application>

     

    Java代码 package reflect   {       import mx.controls.Alert;       import mx.utils.ObjectProxy;                     public dynamic class ReflectTest extends ObjectProxy       {           public function ReflectTest()           {           }                      public function flex():void{               mx.controls.Alert.show("hello");                          }           public function test(args:String):void{               Alert.show("参数是"+args);           }                    }   }   package reflect { import mx.controls.Alert; import mx.utils.ObjectProxy; public dynamic class ReflectTest extends ObjectProxy { public function ReflectTest() { } public function flex():void{ mx.controls.Alert.show("hello"); } public function test(args:String):void{ Alert.show("参数是"+args); } } }

     

     

    Java代码 package objectProxy   {       import flash.utils.flash_proxy;              import mx.utils.ObjectProxy;       import mx.utils.object_proxy;       //public namespace std="hello";       use namespace object_proxy;       use namespace flash_proxy;                    //不继承也行。这也是测试类。同上边基本没关系。这部继承也没关系       public dynamic class ObjectTest extends ObjectProxy       {           public function ObjectTest(item:Object=null, uid:String=null, proxyDepth:int=-1)           {               super(item, uid, proxyDepth);               this._item=item;             }           private var _item:Object;                      public function callMethod(name:*,...rest):*{               callProperty(name,rest);                          }           override flash_proxy function callProperty(name:*, ... rest):*           {               return _item[name].apply(_item,rest);           }                      public function callMethod2(methodName:String,array:Array):void{               _item[methodName].call(_item,array);           }       }   }   package objectProxy { import flash.utils.flash_proxy; import mx.utils.ObjectProxy; import mx.utils.object_proxy; //public namespace std="hello"; use namespace object_proxy; use namespace flash_proxy; //不继承也行。这也是测试类。同上边基本没关系。这部继承也没关系 public dynamic class ObjectTest extends ObjectProxy { public function ObjectTest(item:Object=null, uid:String=null, proxyDepth:int=-1) { super(item, uid, proxyDepth); this._item=item; } private var _item:Object; public function callMethod(name:*,...rest):*{ callProperty(name,rest); } override flash_proxy function callProperty(name:*, ... rest):* { return _item[name].apply(_item,rest); } public function callMethod2(methodName:String,array:Array):void{ _item[methodName].call(_item,array); } } }

     

    Java代码 <?xml version="1.0" encoding="utf-8"?>   <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">       <mx:Script>           <![CDATA[               import objectProxy.ObjectTest;               import mx.rpc.remoting.mxml.RemoteObject;               import reflect.ReflectTest;               import mx.utils.ObjectProxy;               private function test():void{                   var reflect:ReflectTest=new ReflectTest;                   var objs:ObjectTest=new ObjectTest(reflect);   //              /objs.flex();                   objs.callMethod("test","hello");                                      objs.callMethod2("test",new Array("haha"));                                  }           ]]>       </mx:Script>              <mx:Button>           <mx:click>               <![CDATA[                   test();               ]]>           </mx:click>       </mx:Button>   </mx:Application>  
    最新回复(0)