Learning ActionScript 2.0 in Adobe Flash |
|
|
|
| Interfaces > Example: Using interfaces | |||
In this example you create a simple interface that you can reuse between many different classes.
To build an interface:
interface IDocumentation {
public function downloadUpdates():Void;
public function checkForUpdates():Boolean;
public function searchHelp(keyword:String):Array;
}
class FlashPaper implements IDocumentation {
}
You see an error that's similar to the following message:
**Error** path\FlashPaper.as: Line 1: The class must implement method 'checkForUpdates' from interface 'IDocumentation'.
class FlashPaper implements IDocumentation {
Total ActionScript Errors: 1 Reported Errors: 1
This error appears because the current FlashPaper class doesn't define any of the public methods that you defined in the IDocumentation interface.
class FlashPaper implements IDocumentation {
private static var __version:String = "1,2,3,4";
public function downloadUpdates():Void {
};
public function checkForUpdates():Boolean {
return true;
};
public function searchHelp(keyword:String):Array {
return []
};
}
This time you don't see any errors appear in the Output panel.
|
NOTE |
You can add as many additional static, public, or private variables or methods as you want to the FlashPaper class file. The interface file defines only a set of minimum methods that must appear within any class that implements that interface. |
searchHelp() method):
interface IDocumentation {
public function downloadUpdates():Void;
public function checkForUpdates():Boolean;
public function searchHelp(keyword:String):Array;
public function addComment(username:String, comment:String):Void;
}
**Error** path\FlashPaper.as: Line 1: The class must implement method 'addComment' from interface 'IDocumentation'.
class FlashPaper implements IDocumentation {
Total ActionScript Errors: 1 Reported Errors: 1
You see the previous error because the FlashPaper.as class file no longer defines all the classes that you outlined in the interface file. To fix this error message, you must either add the addComment() method to the FlashPaper class or remove the method definition from the IDocumentation interface file.
public function addComment(username:String, comment:String):Void {
/* Send parameters to server-side page, which inserts comment into database. */
}
In the previous section, you created a class-based on the IDocumentation interface file. In this section you create a new class that also implements the IDocumentation interface, although it adds some additional methods and properties.
This tutorial demonstrates the usefulness of using interfaces because if you want to create another class that extends the IDocumentation interface, you can easily identify the methods that are required within the new class.
|
|
|
|