Apache flex 如何判断实例是否在ActionScript 3.0中实现了接口

Apache flex 如何判断实例是否在ActionScript 3.0中实现了接口,apache-flex,actionscript-3,air,Apache Flex,Actionscript 3,Air,我正在重写数组集合的addItem()函数,我想检测添加的项是否实现了特定的接口 <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo" minWidth="1024" m

我正在重写数组集合的addItem()函数,我想检测添加的项是否实现了特定的接口

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo" minWidth="1024" minHeight="768" creationComplete="application1_creationCompleteHandler(event)">
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
            public var test:TestInterface = new TestInterface() //implements ITestInterface


            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {
                trace(test is ITestInterface); //true
            }

        ]]>
    </fx:Script>
</s:Application>
以前我使用,
is
操作符来检测类类型,但现在我使用类的接口,我更愿意测试对象是否实现了接口

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo" minWidth="1024" minHeight="768" creationComplete="application1_creationCompleteHandler(event)">
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
            public var test:TestInterface = new TestInterface() //implements ITestInterface


            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {
                trace(test is ITestInterface); //true
            }

        ]]>
    </fx:Script>
</s:Application>
我希望我可以尝试将对象强制转换为接口,看看它是否为
null
。这是最好的方法吗


我还可以创建一个新的
addFunction()
,它只接受接口类型的对象。

您仍然可以使用is来测试接口

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo" minWidth="1024" minHeight="768" creationComplete="application1_creationCompleteHandler(event)">
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
            public var test:TestInterface = new TestInterface() //implements ITestInterface


            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {
                trace(test is ITestInterface); //true
            }

        ]]>
    </fx:Script>
</s:Application>


添加到Joel的答案中:如果您想了解更多关于类实现的接口(及其子类、父类等)的信息,库中有一个类,它有许多方便的方法。

谢谢Joel,我很懒,但我想知道正确的方法,而不是找到一些有用但可能是坏习惯的东西。好选择!你能在标题末尾加上“在AS3中”吗?