Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if an instance implements an interface in ActionScript 3.0

I am overriding the addItem() function of an array collection and I would like to detect if the added item implements a particular interface.

Previously I used the, is operator to detect the class type, but now that I am using an interface for classes I would rather test to see if the object implements the interface.

I expect I could just try and cast the object as the interface and see if it's not null. Is this the best way to do it?

I could also just create a new addFunction() that only accepts objects of the interface type.

like image 406
robmcm Avatar asked Sep 03 '09 17:09

robmcm


People also ask

WHAT IS interface in ActionScript?

An interface is a collection of method declarations that allows unrelated objects to communicate with one another. For example, ActionScript 3.0 defines the IEventDispatcher interface, which contains method declarations that a class can use to handle event objects.

Can instance be created for interface?

You cannot create an instance of an interface , because an interface is basically an abstract class without the restriction against multiple inheritance. And an abstract class is missing parts of its implementation, or is explicitly marked as abstract to prohibit instantiation.

What is interface and types of interface?

(n.) A boundary across which two independent systems meet and act on or communicate with each other. In computer technology, there are several types of interfaces. user interface – the keyboard, mouse, menus of a computer system. The user interface allows the user to communicate with the operating system.

What is a class in ActionScript?

Classes are external ActionScript files that define objects; they are stored and organized in a specific directory structure in the Flash CS3 program directory (Figure 4.1). To use them in your code, you have to import them.


2 Answers

You can still use is to test for an interface.

<?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>
like image 103
Joel Hooks Avatar answered Nov 12 '22 01:11

Joel Hooks


To add to the answer of Joel: if you want more information about the interfaces a class implements (and its subclasses, parent classes, etc), the AS3Commons library has a ClassUtils class that has a number of convenience methods.

like image 43
Christophe Herreman Avatar answered Nov 12 '22 00:11

Christophe Herreman