简体   繁体   English

检查类是否实现特定接口

[英]Check if Class Implements a Specific Interface

in ActionScript 3.0, there are a few ways to check a class's extension. 在ActionScript 3.0中,有几种方法可以检查类的扩展名。 for example, if i want to know of a custom class extends Sprite i could use the is operator: 例如,如果我想知道自定义类扩展了Sprite,则可以使用is运算符:

trace(MyClass is Sprite);

or i could use flash.utils.getQualifiedSuperclassName : 或者我可以使用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);

why not just trace(MyClass is IMyInterface); 为什么不仅仅trace(MyClass is 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. 如果该类正在实现接口,则此函数返回true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM