AS3 - 我可以知道,如果一个类实现一个接口(或者是另一个类的子类)? [英] AS3 - Can I know if a class implements an interface (or is a subclass of another class)?

查看:372
本文介绍了AS3 - 我可以知道,如果一个类实现一个接口(或者是另一个类的子类)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过这个code

function someFunction(classParam:Class):Boolean
{
    // how to know if classParam implements some interface?
}

即。比较 classParam IEventDispatcher,请接口:

someFunction(EventDispatcher) // returns true
someFunction(Object) // returns false

我知道它不能与做的是运营商。但是,有没有办法做到这一点?有没有办法知道,如果一个类实现某个接口? (或者是其他类的子类?)

I know it can't be done with is operator. But, is there a way to do it? Is there a way to know if a class implements some interface? (or is a subclass of another class?)

可能的解决方案:

一个。创建 classParam 的对象,并使用该对象使用比较运营商。

A. Creating an object of classParam and using that object to compare using is operator.

function someFunction(classParam:Class):Boolean
{
    return (new classParam()) is IEventDispatcher
}

乙。使用不如describeType()

function someFunction(classParam:Class):Boolean
{
    var xml:XML = describeType(classParam)
    // found "implementsInterface" value in xml and compare to IEventDispatcher
}

有一种方法不使用不如describeType 或创建一个运营商?

There is a way that DOES NOT USE describeType or creates a new operator?

推荐答案

我看不出有任何的方式来实现你想要做的只是用什么不如describeType 。照片 它已经为此创建了,你为什么不希望使用它?

I don't see any way to achieve what you're trying to do except by using describeType.
It has been created for this purpose, why don't you want to use it?

编辑:
它实际上只需要2行要做到这一点:


It actually only takes 2 lines to do this :

var classDescription:XML = describeType(classParam);
return (classDescription.factory.implementsInterface.(@type == getQualifiedClassName(IEventDispatcher)).length() != 0);

...或者只是一个,如果它是什么困扰你:

...or just in one, if it's what bothers you:

return (describeType(classParam).factory.implementsInterface.(@type == getQualifiedClassName(IEventDispatcher)).length() != 0);

这篇关于AS3 - 我可以知道,如果一个类实现一个接口(或者是另一个类的子类)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆