
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
226
|
Chapter 8: Interfaces
Sounds reasonable, right? Almost. But there’s a problem. What if a class wishing to
receive events from OrderProcessor already extends another class? For example, sup-
pose the OrderUI class extends MovieClip:
class OrderUI extends MovieClip {
public function onOrderError ( ) {
// Display problem report on screen, not shown...
}
}
In ActionScript, a single class cannot extend more than one class. The OrderUI class
already extends MovieClip, so it can’t extend OrderListener. Hence, instances of
OrderUI can’t register to receive events from OrderProcessor. What we really need in
this situation is a way to indicate that OrderUI instances actually belong to two
datatypes: OrderUI and OrderListener.
Enter...interfaces!
Interfaces and Multidatatype Classes
Earlier, we created the OrderListener datatype by defining an OrderListener class.
That approach forces every OrderProcessor event consumer to be an instance of
either OrderListener or an OrderListener subclass. To loosen that restriction, we can
create the OrderListener datatype by defining an OrderListener interface rather than
an OrderListener class. That way, any class can agree to provide an implementation
for OrderListener while still inheriting from any other class. Let’s see how this works.
Syntactically,