User Guide

250 Chapter 10: Creating Custom Classes with ActionScript 2.0
Encapsulation is why ActionScript 2.0 includes, for example, member access control, so details of
the implementation can be made private and invisible to code outside an object. The code outside
the object is forced to interact with the objects programming interface rather than with the
implementation details. This approach provides some important benefits; for example, it lets the
creator of the object change the objects implementation without requiring any changes to code
outside of the object, as long as the programming interface doesnt change.
Polymorphism
Object-oriented programming lets you express differences between individual classes using a
technique called polymorphism, by which classes can override methods of their superclasses and
define specialized implementations of those methods.
For example, you might start with a class called Mammal that has
play() and sleep() methods.
You then create Cat, Monkey, and Dog subclasses to extend the Mammal class. The subclasses
override the
play() method from the Mammal class, to reflect the habits of those particular kinds
of animals. Monkey implements the
play() method to swing from trees; Cat implements the
play() method to pounce at a ball of yarn; Dog implements the play() method to fetch a ball.
Because the
sleep() functionality is similar between the animals, you would use the superclass
implementation.
Using classes: a simple example
For those who are new to object-oriented programming, this section describes the workflow
involved in creating and using classes in Flash and walks you through a simple hands-on example.
You can also look at another simple example that describes how to assign a custom class to a
movie clip symbol; see Assigning a class to a movie clip symbol” on page 218.
At a minimum, the workflow for creating classes involves the following steps:
1.
Defining a class in an external ActionScript class file.
2.
Saving the class file to a designated classpath directory (a location where Flash looks for classes)
or at least in the same directory as the application’s FLA file. Creating an instance of the class
in another script, either in a Flash (FLA) document or an external script file, or creating a
subclass based on the original class.
This section also discusses a feature in ActionScript 2.0 called strict data typing, which lets you
specify the data type for a variable, function parameter, or function return type.
Although this section discusses only classes, the general workflow is the same for using interfaces.
For more information, see “Creating and using interfaces” on page 261.
The following sections contains code examples that you can use to become familiar with creating
classes in ActionScript 2.0. If youre not familiar with ActionScript 2.0 scripting, see
ActionScript Basics” on page 23 and “Using Best Practices” on page 65.
For more information on using classes, see the following topics:
“Creating a class file” on page 251
“Creating an instance of the Person class” on page 253