Luc*_*hez 9 oop class actionscript-3
有了这段代码
function someFunction(classParam:Class):Boolean
{
// how to know if classParam implements some interface?
}
Run Code Online (Sandbox Code Playgroud)
即classParam与IEventDispatcher界面比较:
someFunction(EventDispatcher) // returns true
someFunction(Object) // returns false
Run Code Online (Sandbox Code Playgroud)
我知道is操作员不能这样做.但是,有没有办法做到这一点?有没有办法知道一个类是否实现了一些接口?(或者是另一个类的子类?)
可能的解决方案:
A. classParam使用is运算符创建对象并使用该对象进行比较.
function someFunction(classParam:Class):Boolean
{
return (new classParam()) is IEventDispatcher
}
Run Code Online (Sandbox Code Playgroud)
B.使用 describeType()
function someFunction(classParam:Class):Boolean
{
var xml:XML = describeType(classParam)
// found "implementsInterface" value in xml and compare to IEventDispatcher
}
Run Code Online (Sandbox Code Playgroud)
有一种方法不使用describeType或创建new运营商?
除了使用之外,我没有看到任何方法来实现你想要做的事情describeType.
它是为此目的而创建的,为什么你不想使用它?
编辑:
它实际上只需要2行:
var classDescription:XML = describeType(classParam);
return (classDescription.factory.implementsInterface.(@type == getQualifiedClassName(IEventDispatcher)).length() != 0);
Run Code Online (Sandbox Code Playgroud)
...或只是在一个,如果它困扰你:
return (describeType(classParam).factory.implementsInterface.(@type == getQualifiedClassName(IEventDispatcher)).length() != 0);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3061 次 |
| 最近记录: |