EasyManua.ls Logo

MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT - Creating and Using Interfaces

MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT
816 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
Creating and using interfaces 167
Class members and subclasses
Class members propagate to subclasses of the superclass that defines those members. In the
previous example (see “Using class members: a simple example” on page 166), you used a class
property to keep track of the number of instances of that class you created. You could create a
subclass of the Widget class, as shown below.
class SubWidget extends Widget {
function SubWidget() {
trace("Creating subwidget # "+Widget.widgetCount);
}
}
Creating and using interfaces
An interface in object-oriented programming is like a class whose methods have been declared,
but otherwise dont do” anything. That is, an interface consists of “empty” methods.
One use of interfaces is to enforce a protocol between otherwise unrelated classes, as discussed
next. For example, suppose youre part of a team of programmers, each of whom is working on a
different part—that is, a different class—of a large application. Most of these classes are unrelated,
but you still need a way for the different classes to communicate. That is, you need to define an
interface, or communication protocol, that all the classes must adhere to.
One way to do this would be to create a Communication class that defines all of these methods,
and then have each class extend, or inherit from, this superclass. But because the application
consists of classes that are unrelated, it doesnt make sense to force them all into a common class
hierarchy. A better solution is to create an interface that declares the methods these classes will use
to communicate, and then have each class implement (provide its own definitions for)
those methods.
You can usually program successfully without using interfaces. When used appropriately,
however, interfaces can make the design of your applications more elegant, scalable,
and maintainable.
Creating an interface
The process for creating an interface is the same as for creating a class. As with classes, you can
only define interfaces in external ActionScript (AS) files. 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.
interface interfaceName {
// interface method declarations
}
An interface can contain only method (function) declarations, including parameters, parameter
types, and function return types.

Table of Contents

Related product manuals