AS3反射
反射这玩意,在一些游戏的框架中的确有其优势,但是注意反射或多或少会影响性能的.
在资源的获取上就 使用了反射 , 得到SWF中的美术的资源 如:
public static function getClazz(className : String,loader : Loader = null) : Class{
if(null == loader){
loader = UI_LOADER;
}
return loader.contentLoaderInfo.applicationDomain.getDefinition(className) as Class;
}
当然,像MovieClip,Sprite等除了 使用 new , 还可以用反射 来使它们实例化.
var mcClass:Class = getDefinitionByName("flash.display.MovieClip") as Class;
var mc:MovieClip = new mcClass() as MovieClip;
扩展一下:这个getDefinitionByName其实很有用,也可以反射自己自定义的类:
package com.ainy.factorymanager{
public class AnimalFactory{
}
}
那么反射 自定义类:
var myClass:Class = getDefinitionByName("com.ainy.factorymanager.AnimalFactory") as Class;
var $my : AnimalFactory = new myClass() as AnimalFactory;
再扩展,可以联想到Java的三大框架当中的反射配置XML,当然也可以制成Json的二进制文件,用AS来读解.
这玩意在某些情况下,确实很好用, 但是 反射 是要多耗性能的.
扩展:
对于getDefinitionByName参数的解释:
①
package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.utils.getDefinitionByName;
public class AonauflyTest extends Sprite
{
public function AonauflyTest()
{
var $reflex_class : Class = getDefinitionByName("flash.text.TextField") as Class;
var $txt_reflex : TextField = new $reflex_class() as TextField;
$txt_reflex.text = "getDefinitionByName(\"flash.text.TextField\")";
$txt_reflex.width = 250;
this.stage.addChild( $txt_reflex );
}
}
}
得到的结果:
②
package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.utils.getDefinitionByName;
public class AonauflyTest extends Sprite
{
public function AonauflyTest()
{
var $reflex_class : Class = getDefinitionByName("flash.text::TextField") as Class;
var $txt_reflex : TextField = new $reflex_class() as TextField;
$txt_reflex.text = "getDefinitionByName(\"flash.text::TextField\")";
$txt_reflex.width = 250;
this.stage.addChild( $txt_reflex );
}
}
}
结果:
可见 flash.text::TextField 和 flash.text.TextField的效果是一样的
返回对象的完全限定名:
①
package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
import flash.utils.getQualifiedSuperclassName;
import avmplus.getQualifiedSuperclassName;
public class AonauflyTest extends Sprite
{
public function AonauflyTest()
{
var $reflex_class : Class = getDefinitionByName("flash.text::TextField") as Class;
var $txt_reflex : TextField = new $reflex_class() as TextField;
// $txt_reflex.text = "getDefinitionByName(\"flash.text::TextField\")";
$txt_reflex.width = 250;
this.stage.addChild( $txt_reflex );
//返回对象的完全限定名
var $str : String = avmplus.getQualifiedClassName( TextField );
$txt_reflex.text = $str;
}
}
}
返回结果:
②
package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedSuperclassName;
import avmplus.getQualifiedSuperclassName;
public class AonauflyTest extends Sprite
{
public function AonauflyTest()
{
var $reflex_class : Class = getDefinitionByName("flash.text::TextField") as Class;
var $txt_reflex : TextField = new $reflex_class() as TextField;
// $txt_reflex.text = "getDefinitionByName(\"flash.text::TextField\")";
$txt_reflex.width = 250;
this.stage.addChild( $txt_reflex );
//返回对象的完全限定名
// var $str : String = avmplus.getQualifiedClassName( TextField );
// $txt_reflex.text = $str;
//返回对象的完全限定名
var $str_1 : String = flash.utils.getQualifiedClassName( TextField );
$txt_reflex.text = $str_1;
}
}
}
返回的结果是一样的(都是::的形式):
返回对象的基类的完全限定名:
引文也有2种方式 , 而且返回的结果都一样,所以直接都写出来:
package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.utils.getDefinitionByName;
import avmplus.getQualifiedSuperclassName;
public class AonauflyTest extends Sprite
{
public function AonauflyTest()
{
var $reflex_class : Class = getDefinitionByName("flash.text::TextField") as Class;
var $txt_reflex : TextField = new $reflex_class() as TextField;
// $txt_reflex.text = "getDefinitionByName(\"flash.text::TextField\")";
$txt_reflex.width = 250;
this.stage.addChild( $txt_reflex );
//①
//返回对象的基类的完全限定名
// var $str_super : String = avmplus.getQualifiedSuperclassName( TextField );
// $txt_reflex.text = $str_super;
//②
//返回对象的基类的完全限定名
var $str_super_1 : String = flash.utils.getQualifiedSuperclassName( TextField );
$txt_reflex.text = $str_super_1;
}
}
}
结果:
一个注意(object是所有类的基类,他没有基类,返回null):
//===========================访问类的公共成员================================================================================
使用 : import flash.utils.describeType;
var xml:XML = describeType(userData); 获取对象的公共(public)成员
package com.model
{
public class BaseModel
{
public function BaseModel()
{
}
private var _id : int;
public function get id() : int
{
return this._id;
}
public function set id( value : int ) : void
{
this._id = value;
}
}
}
package com.model
{
public final class UserModel extends BaseModel
{
public function UserModel()
{
super();
}
private var _name : String;
public function set name( value : String ) : void
{
this._name = value;
}
public function get name() : String
{
return this._name;
}
public var _sex : int = 0;
private function get sex( ) : int
{
return _sex;
}
private function set sex( value : int ) : void
{
this._sex = value;
}
private var _occupation : int = 0;
private function get occupation():int
{
return this._occupation;
}
public function set occupation( value : int ) : void
{
this._occupation = value;
}
public function doSomething() : void
{
}
private function doPrivateSomething():void
{
}
public function sum( a : int , b : int = 1 ) : int
{
return a + b;
}
}
}
重点 :
package
{
import com.model.UserModel;
import flash.display.Sprite;
import flash.utils.describeType;
public class AonauflyTest extends Sprite
{
public function AonauflyTest()
{
var userData : UserModel = new UserModel();
var xml:XML = describeType(userData);
var propMap : Object = new Object();
// for(var i:int =0;i<xml.accessor.length();i++){
// trace("name :"+xml.accessor[i].@name);//获取类的属性名
// trace("value : "+userData[xml.accessor[i].@name]);//获取类属性值
// }
for each (var item :XML in xml.*) {
var itemName : String = item.name().toString();
switch(itemName) {
case "variable":
propMap["字段:" +item.@name.toString()] = item.@type.toString();
break;
case "accessor":
var access : String = item.@access;
if((access == "readwrite") || (access == "writeonly")) {
propMap["属性:"+item.@name.toString()] = item.@type.toString();
}
break;
}
}
for (var prop:String in propMap)
{
trace(prop, "-", propMap[prop]);
}
trace("========================================================");
trace("xml : "+xml.toXMLString());//遍历转换xml格式的数据
}
}
}
结果 :
属性:occupation - int
属性:id - int
属性:name - String
字段:_sex - int
========================================================
xml : <type name="com.model::UserModel" base="com.model::BaseModel" isDynamic="false" isFinal="true" isStatic="false">
<extendsClass type="com.model::BaseModel"/>
<extendsClass type="Object"/>
<variable name="_sex" type="int">
<metadata name="__go_to_definition_help">
<arg key="pos" value="340"/>
</metadata>
</variable>
<accessor name="name" access="readwrite" type="String" declaredBy="com.model::UserModel">
<metadata name="__go_to_definition_help">
<arg key="pos" value="273"/>
</metadata>
<metadata name="__go_to_definition_help">
<arg key="pos" value="186"/>
</metadata>
</accessor>
<accessor name="id" access="readwrite" type="int" declaredBy="com.model::BaseModel">
<metadata name="__go_to_definition_help">
<arg key="pos" value="146"/>
</metadata>
<metadata name="__go_to_definition_help">
<arg key="pos" value="215"/>
</metadata>
</accessor>
<accessor name="occupation" access="writeonly" type="int" declaredBy="com.model::UserModel">
<metadata name="__go_to_definition_help">
<arg key="pos" value="648"/>
</metadata>
</accessor>
<method name="sum" declaredBy="com.model::UserModel" returnType="int">
<parameter index="1" type="int" optional="false"/>
<parameter index="2" type="int" optional="true"/>
<metadata name="__go_to_definition_help">
<arg key="pos" value="850"/>
</metadata>
</method>
<method name="doSomething" declaredBy="com.model::UserModel" returnType="void">
<metadata name="__go_to_definition_help">
<arg key="pos" value="744"/>
</metadata>
</method>
<metadata name="__go_to_ctor_definition_help">
<arg key="pos" value="93"/>
</metadata>
<metadata name="__go_to_definition_help">
<arg key="pos" value="42"/>
</metadata>
</type>
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341