Creating And Using Interfaces - MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT Reference Manual

Actionscript reference guide
Hide thumbs Also See for FLASH MX 2004 - ACTIONSCRIPT:
Table of Contents

Advertisement

Class members and subclasses
Class members propagate to subclasses of the superclass that defines those members. In the
previous example (see
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 don't "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 you're 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 doesn't 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
keyword, followed by the interface name, and then left and right curly braces, which
interface
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.
"Using class members: a simple example" on page
166), you used a class
Creating and using interfaces
167

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the FLASH MX 2004 - ACTIONSCRIPT and is the answer not in the manual?

Questions and answers

Table of Contents