User Guide

262 Chapter 10: Creating Custom Classes with ActionScript 2.0
For example, the following code declares an interface named MyInterface that contains two
methods,
method_1() and method_2(). The first method, method_1(), has no parameters and
specifies a return type of
Void (meaning it does not return a value). The second method,
method_2(), has a single parameter of type String, and specifies a return type of Boolean.
interface MyInterface {
function method_1():Void;
function method_2(param:String):Boolean;
}
Interfaces cannot contain any variable declarations or assignments. Functions declared in an
interface cannot contain curly braces. For example, the following interface wont compile.
interface BadInterface{
// Compiler error. Variable declarations not allowed in interfaces.
var illegalVar;
// Compiler error. Function bodies not allowed in interfaces.
function illegalMethod(){
}
}
You can also use the extends keyword to create subclasses of an interface:
interface iA extends interface iB {}
The rules for naming interfaces and storing them in packages are the same as those for classes; see
“Creating and using classes” on page 254 and “Using packages” on page 260.
Interfaces as data types
Like a class, an interface defines a new data type. Any class that implements an interface can be
considered to be of the type defined by the interface. This is useful for determining if a given
object implements a given interface. For example, consider the following interface:
interface Movable {
function moveUp():Void;
function moveDown():Void;
}
Now consider the class Box, which implements the Movable interface.
class Box implements Movable {
var xpos:Number;
var ypos:Number;
function moveUp():Void {
trace("moving up");
// method definition
}
function moveDown():Void {
trace("moving down");
// method definition
}
}