开发者

Check if Class Implements a Specific Interface

in ActionScript 3.0, there are a few ways to check a class's extension. for exampl开发者_Python百科e, if i want to know of a custom class extends Sprite i could use the is operator:

trace(MyClass is Sprite);

or i could use flash.utils.getQualifiedSuperclassName:

trace(getQualifiedSuperclassName(MyClass));

i would like to accept a class as an argument and check to see if the passed class implements an certain interface. is there an equally simple or common way to check if my custom class adheres to an interface? perhaps something like:

trace(MyClass implements IMyInterface);


Use something like this function:

public function isImplementing( MyClass:Class, MyInterface:Class ):Boolean
{
    var description:XML = describeType( MyClass );
    var interfaceName:String = getQualifiedClassName( MyInterface );
    return Boolean( description.factory.implementsInterface.( @type == interfaceName ).length() != 0 );
}

This function returns true if the class is implementing the interface.


why not just trace(MyClass is IMyInterface);?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜