[ActionScript 3.0] 如何获得实例对象的类名及类

 1 package

 2 {

 3     import flash.display.DisplayObject;

 4     import flash.display.MovieClip;

 5     import flash.display.Sprite;

 6     import flash.events.Event;

 7     import flash.events.MouseEvent;

 8     import flash.utils.getDefinitionByName;

 9     import flash.utils.getQualifiedClassName;

10     

17     public class QualifiedClass extends Sprite

18     {

19         private var _mc:MovieClip;

20         

21         public function QualifiedClass()

22         {

23             this.graphics.beginFill(0x000000);

24             this.graphics.drawRect(0,0,500,500);

25             this.graphics.endFill();

26             _mc = new MovieClip();

27             _mc.graphics.beginFill(0x00ff00);

28             _mc.graphics.drawRect(0,0,50,50);

29             this.addChild(_mc);

30             stage.addEventListener(MouseEvent.CLICK,onClick);

31             _mc.addEventListener(MouseEvent.CLICK,onClick);

32         }

33         private function onClick(e:MouseEvent):void

34         {

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

36             trace("click");

37             trace(e.target.name); //返回instance**之类的

38             trace(getQualifiedClassName(e.target)); //返回元件名

39             

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

41             trace(A.prototype.constructor);      // [class A] 

42             trace(A.prototype.constructor == A); // true 

43             var myA:A = new A(); 

44             trace(myA.constructor == A);         // true 

45             

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

47             var ClassReference:Class = getDefinitionByName("flash.display.Sprite") as Class; 

48             var instance:Object = new ClassReference(); 

49             instance.graphics.beginFill(0xff0000); 

50             instance.graphics.drawRect(50, 50, 50, 50); 

51             instance.graphics.endFill(); 

52             addChild(DisplayObject(instance)); 

53         }

54         

55     }

56 }

57 dynamic class A {} 

 

你可能感兴趣的:(actionscript)