Interfaces
Interfaces in object-oriented programming can be described as classes whose methods are not
implemented (defined). Another class can implement the methods declared by the interface.
An interface can also be thought of as a "programming contract" that can be used to enforce
relationships between otherwise unrelated classes. For example, suppose you are working with a
team of programmers, each of whom is working on a different part (class) of the same application.
While designing the application, you agree on a set of methods that the different classes will use
to communicate. So you create an interface that declares these methods, their parameters, and
their return types. Any class that implements this interface must provide definitions for those
methods; otherwise, a compiler error will result.
You can also use interfaces to provide a limited form of "multiple inheritance," which is not
allowed in ActionScript 2.0. In multiple inheritance, a class extends more than one class. For
example, in C++ the Cat class could extend the Mammal class, as well as a Playful class, which has
methods ChaseTail and EatCatNip. ActionScript 2.0, like Java, does not allow a class to extend
multiple classes directly. However, you could create a Playful interface that declares the ChaseTail
and EatCatNip methods. A Cat class, or any other class, could then implement this interface and
provide definitions for those methods.
For more information, see
Using classes: a simple example
For those who are new to object-oriented programming, this section provides an overview of the
workflow involved in creating and using classes in Flash. At a minimum, this workflow involves
the following steps:
Defining a class in an external ActionScript class file.
1
Saving the class file to a designated classpath directory (a location where Flash looks for classes).
2
Creating an instance of the class in another script, either in a Flash (FLA) document or an
3
external script file, or creating a subclass based the original class.
Also discussed in this section is a new feature in ActionScript 2.0 called strict data typing, which
lets you specify the data type for a variable, function parameter, or function return type.
Although this section discusses only classes, the general workflow is the same for using interfaces.
For more information, see
Creating a class file
To create a class, you must first create an external ActionScript (AS) file. Classes (and interfaces)
can only be defined in external script files. For example, you can't define a class in a script
attached to a frame or button in a Flash document (FLA). To create an external AS file, use the
ActionScript editor included with Flash or your preferred code or text editor.
Note: ActionScript code in external files is compiled into a SWF file when you publish, export, test, or
debug a FLA file. Therefore, if you make any changes to an external file, you must save the file and
recompile any FLA files that use it.
In the steps below you'll create a class called Person that contains two properties (
and a single method (
Output panel.
"Creating an interface" on page
"Creating and using interfaces" on page
) that displays the values of those properties in the
showInfo()
167.
167.
Using classes: a simple example
and
)
age
name
157
Need help?
Do you have a question about the FLASH MX 2004 - ACTIONSCRIPT and is the answer not in the manual?