Interfaces and Events with ActionScript and Flex (Page 1 of 4 )
Interfaces
ActionScript 3.0 also allows you to define interfaces. Interfaces allow you to separate the interface from the implementation, which enables greater application flexibility.
Much of what you learned about declaring classes applies to declaring interfaces as well. In fact, it’s easier to list the differences:
Interfaces use theinterfacekeyword rather than theclasskeyword.
Interfaces cannot declare properties.
Interface methods declare the method signature but not the implementation.
Interfaces declare only thepublicinterface for implementing classes, and therefore method signature declarations do not allow for modifiers.
By convention, interface names start with an uppercaseI. The following is an example of an interface:
package com.example { public interface IExample { function a():String; function b(one:String, two:uint):void; } }
In the preceding example,interfacesays that any implementing class must declare methodsa()andb()using the specified signatures.
You can declare a class so that it implements an interface using theimplementskeyword, following the class name or following the superclass name if the class extends a superclass. The following example implementsIExample:
package com.example { import com.example.IExample; public class Example implements IExample { public function Example() { } public function a():String { return "a"; } public function b(one:String, two:uint):void { trace(one + " " + two); } } }
When a class implements an interface, the compiler verifies that it implements all the required methods. If it doesn’t, the compiler throws an error. A class can implement methods beyond those specified by an interface, but it must always implement at least those methods. A class can also implement more than one interface with a comma-delimited list of interfaces following theimplementskeyword.