ActionScript 3 : Get a Class Reference by Class Name

来源:互联网 发布:oracle数据库linux安装 编辑:程序博客网 时间:2024/05/17 06:52

ActionScript 3 : Get a Class Reference by Class Name

with 37 comments

If you need to get a reference to a class in ActionScript 3, but only know the class name, then you can use the flash.utils.getDefinitionByName to create an instance of the class.

For example:

package{import flash.display.Sprite;import flash.utils.getDefinitionByName;public class DynamicCall extends Sprite{public function DynamicCall(){            var ClassReference:Class = getDefinitionByName("String") as Class;            var s:String = (new ClassReference("foo=") as String);            trace(s);}}}

This basically creates an instance of the String class, from the class name “String”. getDefinitionByName takes the entire class path, so if you wanted to create an instance of MovieClip, you would provide the entire path:

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

Anyways, pretty simple stuff, but can come in useful.

http://www.mikechambers.com/blog/2006/06/22/actionscript-3-get-a-class-reference-by-class-name/
原创粉丝点击