Check if Class Implements a Specific Interface

浪子不回头ぞ 提交于 2019-12-24 05:24:10

问题


in ActionScript 3.0, there are a few ways to check a class's extension. for example, 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);


回答1:


why not just trace(MyClass is IMyInterface);?




回答2:


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.



来源:https://stackoverflow.com/questions/7327495/check-if-class-implements-a-specific-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!