Learning ActionScript 2.0 in Adobe Flash |
|
|
|
| Interfaces > Example: Creating a complex interface | |||
The following example shows several ways to define and implement interfaces. In this tutorial you learn how to create a simple interface file and how to write a class that implements multiple interfaces, as well as how to have interfaces extend other interfaces to create more complex data structures.
To create a complex interface:You save all of the files you create for this tutorial in this directory.
// filename: InterfaceA.as
interface InterfaceA {
public function k():Number;
public function n(z:Number):Number;
}
ClassB.as implements the InterfaceA interface you created previously.
// filename: ClassB.as
class ClassB implements InterfaceA {
public function k():Number {
return 25;
}
public function n(z:Number):Number {
return (z + 5);
}
}
This class file tests the ClassB class you created previously.
// filename: classbTest.fla import ClassB; var myB:ClassB = new ClassB(); trace(myB.k()); // 25 trace(myB.n(7)); // 12
The Output panel displays two numbers, 25 and 12, which are the results of the k() and n() methods in the ClassB class.
This class file implements the InterfaceA interface that you created in step 1.
// filename: ClassC.as
class ClassC implements InterfaceA {
public function k():Number {
return 25;
}
// **Error** The class must also implement method 'n' from interface 'InterfaceA'.
}
If you click the Check Syntax button for the ClassC class file, Flash displays an error message in the Output panel that says the current class must implement the n() method defined in the InterfaceA interface. When you create classes that implement an interface, it is important that you define methods for each entry in the interface.
// filename: InterfaceB.as
interface InterfaceB {
public function o():Void;
}
This class implements both the InterfaceA interface and the InterfaceB interface you created in earlier steps. The ClassD class must include method implementations for each of the methods listed in each of the interface files.
// filename: ClassD.as
class ClassD implements InterfaceA, InterfaceB {
public function k():Number {
return 15;
}
public function n(z:Number):Number {
return (z * z);
}
public function o():Void {
trace("o");
}
}
This Flash document tests the ClassD class that you created previously.
// filename: classdTest.fla import ClassD; var myD:ClassD = new ClassD(); trace(myD.k()); // 15 trace(myD.n(7)); // 49 myD.o(); // o
The values 15 and 49 and the letter o should be displayed in the Output panel. These values are the results of the ClassD.k() method, ClassD.n(), and ClassD.o() methods, respectively.
This interface extends the InterfaceA interface you created earlier, and it adds a new method definition.
// filename: InterfaceC.as
interface InterfaceC extends InterfaceA {
public function p():Void;
}
This class implements two interfaces, InterfaceB and InterfaceC.
// filename: ClassE.as
class ClassE implements InterfaceB, InterfaceC {
public function k():Number {
return 15;
}
public function n(z:Number):Number {
return (z + 5);
}
public function o():Void {
trace("o");
}
public function p():Void {
trace("p");
}
}
// filename: classeTest.fla import ClassE; var myE:ClassE = new ClassE(); trace(myE.k()); // 15 trace(myE.n(7)); // 12 myE.o(); // o myE.p(); // p
The values 15, 12, o, and p display in the Output panel. These values are the values that return from the ClassE.k(), ClassE.n(), ClassE.o(), and ClassE.p() methods. Since the ClassE class implemented both the InterfaceB and InterfaceC interfaces, each method from the two interface files must be defined. Although the InterfaceB and InterfaceC interfaces only define the o() and p() methods, InterfaceC extends InterfaceA. This means that all of its defined methods, k() and n(), must also be implemented.
|
|
|
|