![](/img/trans.png)
[英]How can I tell if an Actionscript object has a certain dynamic property on it?
[英]How can I tell if an instance implements an interface in ActionScript 3.0
我重寫了數組集合的addItem()函數,我想檢測添加的項是否實現了特定的接口。
以前我使用, is
運算符來檢測類類型,但現在我正在使用類的接口,我寧願測試以查看對象是否實現了接口。
我希望我可以嘗試將對象轉換為接口並查看它是否為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的答案:如果您想了解有關類實現的接口(及其子類,父類等)的更多信息, AS3Commons庫有一個ClassUtils類,它具有許多便捷方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.