316
Interfaces
Defining and implementing interfaces
The process for creating an interface is the same as for creating a class. Like classes, you can
define interfaces only in external ActionScript files. At a minimum, the workflow for creating
an interface involves the following steps:
■
Defining a interface in an external ActionScript file
■
Saving the interface file to a designated classpath directory (a location where Flash looks
for classes) or in the same directory as the application’s FLA file
■
Creating an instance of the class in another script, either in a Flash (FLA) document or an
external script file, or subinterfaces based on the original interface
■
Creating a class that implements the interface in an external script file
You declare an interface using the
interface
keyword, followed by the interface name, and
then left and right curly braces (
{}
), which define the body of the interface, as shown in the
following example:
interface IEmployeeRecords {
// interface method declarations
}
An interface can contain only method (function) declarations, including parameters,
parameter types, and function return types.
For more information on conventions for structuring classes and interfaces, see
Chapter 19,
“Best Practices and Coding Conventions for ActionScript 2.0,” on page 731
. For a tutorial on
creating an application that uses an interface, see
“Example: Using interfaces” on page 321
.
For example, the following code declares an interface named
IMyInterface
that contains two
methods,
method1()
and
method2()
. The first method,
method1()
, has no parameters and
specifies a return type of Void (meaning that it does not return a value). The second method,
method2()
, has a single parameter of type String, and specifies a return type of Boolean.
To create a simple interface:
1.
Create a new ActionScript file and save it as
IMyInterface.as
.
2.
Type the following ActionScript code into the Script window:
interface IMyInterface {
public function method1():Void;
public function method2(param:String):Boolean;
}
3.
Save your changes to the ActionScript file.
In order to use the interface within an application, you first need to create a class that
implements your new interface.
Содержание FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Страница 1: ...Learning ActionScript 2 0 in Flash...
Страница 8: ...8 Contents...
Страница 18: ...18 Introduction...
Страница 30: ...30 What s New in Flash 8 ActionScript...
Страница 66: ...66 Writing and Editing ActionScript 2 0...
Страница 328: ...328 Interfaces...
Страница 350: ...350 Handling Events...
Страница 590: ...590 Creating Interaction with ActionScript...
Страница 710: ...710 Understanding Security...
Страница 730: ...730 Debugging Applications...
Страница 780: ...780 Deprecated Flash 4 operators...
Страница 830: ...830 Index...