问题
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