|
|
||||||||
| Ultrashock Tutorials > Flash MX 2004 > ActionScript 2.0 | ||||||||
|
||||||||
|
|
ActionScript 2.0 |
|
||||||
11. InterfaceWhen it comes to OOP, besides inheritance, another useful concept is the interface. Like Java and C#, ActionScript does not support multiple inheritance (i.e. a subclass with more than one superclass). This is because multiple inheritance can be a nightmare to debug if used improperly, getting into circular referencing and other sticky situations. In a carefully designed application, using interfaces can achieve similar results with less headaches. An interface is a contract that specifies what methods must be implemented by a class implementing it; usually there is no relationship between the interface and the implementing class. In an interface, there is no code or property, only method signatures. Interfaces are written almost like classes, except:
Here is an example of a Draggable class (the name of an interface is usually prefixed with the letter 'I'): interface IDraggable {
public function startDrag():Void;
public function stopDrag():Void;
public function isDragging():Boolean;
}
Use the implements keyword to use an interface. For example, the Child class
inherits from the Parent class, and implements from the IDraggable interface:
class Child extends Parent implements IDraggable {
public function Child() {
// add Child constructor code
}
public function startDrag():Void {
// implement the code for startDrag();
}
public function stopDrag():Void {
// implement the code for stopDrag();
}
public function isDragging():Boolean {
// implement the code for isDragging();
}
}
The only thing to keep in mind with interfaces is that all the declared
methods must be implemented with the declared methods signatures (i.e. same
arguments and types). In a way, interfaces standardize code between different
developers. When a known interface is used by a class, the developer only
needs to know the interface in order to implement the same in different
classes.
|
||||||||
©2003 Ultrashock.com - All rights reserved |