Learning ActionScript 2.0 in Adobe Flash |
|
|
|
| Interfaces > About 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:
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 Best Practices and Coding Conventions for ActionScript 2.0. For a tutorial on creating an application that uses an interface, see Example: Using interfaces.
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:
interface IMyInterface {
public function method1():Void;
public function method2(param:String):Boolean;
}
In order to use the interface within an application, you first need to create a class that implements your new interface.
class MyClass {
}
In order to instruct the custom class (MyClass) to use your interface (IMyInterface), you need to use the implements keyword, which specifies that a class must define all the methods declared in the interface (or interfaces) that you implement.
class MyClass implements IMyInterface {
}
You place the implements keyword after the class name.
Flash displays an error in the Output panel stating that MyClass must implement method X from interface IMyInterface. You see this error message because any class that extends an interface must define each method that's listed in the interface document.
method1() and method2() methods, as shown in the following snippet:
class MyClass implements IMyInterface {
public function method1():Void {
// ...
};
public function method2(param:String):Boolean {
// ...
return true;
}
}
The Output panel no longer displays any error messages or warnings because you have now defined the two methods.
The class file that you create is not limited to the public methods that you define in the interface file. The interface file only outlines the minimum methods that you must implement, as well as those methods' properties and return types. Classes that implement a particular interface almost always include additional methods, variables, and getter and setter methods.
Interface files cannot contain any variable declarations or assignments. Functions that you declare in an interface cannot contain curly braces. For example, the following interface does not compile:
interface IBadInterface {
// Compiler error. Variable declarations not allowed in interfaces.
public var illegalVar:String;
// Compiler error. Function bodies not allowed in interfaces.
public function illegalMethod():Void {
}
// Compiler error. Private methods are not allowed in interfaces.
private function illegalPrivateMethod():Void;
// Compiler error. Getters/setters are not allowed in interfaces.
public function get illegalGetter():String;
}
For a tutorial demonstrating how to create a complex interface, see Example: Using interfaces.
The rules for naming interfaces and storing them in packages are the same as those for classes; see About naming class files.
|
|
|
|