您当前的位置: 首页 >  Jave.Lin

as3 判断,某个类是否继承于某个类,某个类是否实现某个接口的方法

Jave.Lin 发布时间:2014-01-20 13:40:40 ,浏览量:3

不多说,直接上代码:

package utils
{
	import flash.system.ApplicationDomain;
	import flash.utils.describeType;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	import flash.utils.getQualifiedSuperclassName;

	/** 
	 * 类型工具
	 * @author Jave.Lin
	 * @date 2014-1-20 
	 **/ 
	public class TypeUtil
	{
		/**
		 * AS3 判断某个类型(注意:不是实例)是否继承于某个类型的方法
		 * @author Jave.Lin
		 * @date 2013-12-25
		 * 
		 * @param targetCls 指定要判断的目标类型
		 * @param specialCls 指定要判断是否继承的类型
		 * @return 如果是,返回true,否则返回false
		 * 
		 * 
		 * 
		 * e.g:
		 * public class A{}
		 * public class B exnteds A {} // 继承于A
		 * 
		 * var result:Boolean = isTargetClsInheritSpecialCls(B, A); // 判断B类型,是否继承于A类型
		 * trace(result); // out put true
		 * 
		 * public class C{} // 没有继承用户指定类型,当然,默认会继承于:Object类型
		 * 
		 * result = isTargetClsInheritSpecialCls(C, B); // 判断C类型,是否继承于B类型
		 * trace(result); // out put false
		 * 
		 * 
		 */		
		public static function isTargetClsInheritSpecialCls(targetCls:Class, specialCls:Class, targetClsDomain:ApplicationDomain = null):Boolean
		{
			if(!targetCls || !specialCls) return false;
			var targetClsStr:String = getQualifiedClassName(targetCls);
			
//			if(targetClsDomain){
//				var testResult:Boolean = targetClsDomain == ApplicationDomain.currentDomain;
//				trace("testResult:" + testResult); // 输出结果有可以是false,由此而得知,调用时的ApplicationDomain.currentDomain,与运行进来时的可能会是不一样,因为程序域不一样
//			}

			targetClsDomain = !targetClsDomain ? ApplicationDomain.currentDomain : targetClsDomain;
			
			while(targetClsStr != null)
			{
				var superCls:Class = targetClsDomain.getDefinition(targetClsStr) as Class;
				if(superCls == specialCls) return true;
				targetClsStr = getQualifiedSuperclassName(superCls);
			}
			return false;
		}
		/**
		 * @author Jave.Lin
		 * @date 2014-1-20
		 */		
		public static function isTargetClsImplementsSpecialItf(targetCls:Class, specialItf:Class, targetClsDomain:ApplicationDomain):Boolean
		{
			if(!targetCls || !specialItf) return false;
			var classDescription:XML = describeType(targetCls);
			return (classDescription.factory.implementsInterface.(@type == getQualifiedClassName(specialItf)).length() != 0);
		}
	}
}
调用DEMO:

class BaseTest
{
	
}

class Test1 extends BaseTest
{
	
}

class Test2 extends Sprite
{
	
}

interface ITest
{
	function test():void;
}

class Test3 extends Sprite implements ITest
{
	public function test():void{}
}

// 测试
var result:Boolean;
// 继承类判断
result = isTargetClsInheritSpecialCls(Test1, BaseTest); // result = true,因为Test1是继承于BaseTest
result = isTargetClsInheritSpecialCls(Test2, BaseTest); // result = false,因为Test2不是继承于BaseTest
result = isTargetClsInheritSpecialCls(Test2, DisplayObject); // result = true,因为Test2继承于Sprite,而Sprite又是从DisplayObject继承下来的关系类;
// 实现接口判断
result = isTargetClsImplementsSpecialItf(Test1, ITest); // result = true,因为Test1未实现ITest接口
result = isTargetClsImplementsSpecialItf(Test2, ITest); // result = false,因为Test2也是未实现ITest接口
result = isTargetClsImplementsSpecialItf(Test3, ITest); // result = true,因为Test3实现了ITest接口

关注
打赏
1688896170
查看更多评论

Jave.Lin

暂无认证

  • 3浏览

    0关注

    546博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0747s