`

AS3动态获得类名及类的方法

阅读更多

1.获得某个实例对象的类名:getQualifiedClassName (实例名字符串表达式);

 

stage.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandle);
 function mouseOverHandle(e:Event):void {
     trace("over");
     //   返回instance**之类的        
     trace(e.target.name);
     //返回元件名   
     trace(getQualifiedClassName(e.target));
 }

 

2.获得某个实例对象的类:constructor

 

dynamic class A {}
trace(A.prototype.constructor);      // [class A]
trace(A.prototype.constructor == A); // true
var myA:A = new A();
trace(myA.constructor == A);         // true

 

3.根据类名获得类: getDefinitionByName(类名);

 

package {
     import flash.display.DisplayObject;
     import flash.display.Sprite;
     import flash.utils.getDefinitionByName;
 
     public class GetDefinitionByNameExample extends Sprite {
         private var bgColor:uint = 0xFFCC00;
         private var size:uint = 80;
 
         public function GetDefinitionByNameExample() {
             var ClassReference:Class = getDefinitionByName("flash.display.Sprite") as Class;
             var instance:Object = new ClassReference();
             instance.graphics.beginFill(bgColor);
             instance.graphics.drawRect(0, 0, size, size);
             instance.graphics.endFill();
             addChild(DisplayObject(instance));
         }
     }
 }

 

4.用得到的类实例化对象:一般中间要用一个无类型变量传递类,进行实例化;如:

 

var i=0; 
var c:*=getDefinitionByName(getQualifiedClassName ("MyClass"+i)); 
var myClass:*=new c();
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics