User Guide

48 Chapter 2: Creating Custom Classes with ActionScript 2.0
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.
At a minimum, the workflow for creating classes involves the following steps:
1.
Defining a class in an ActionScript class file.
2.
Saving the class file to the directory that is in the ActionScript classpath (a location where Flex
looks for classes).
3.
Creating an instance of the class in another script, 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 58.
For more information on using classes, see the following topics:
“Creating a class file” on page 48
“Creating an instance of the Person class” on page 50
Creating a class file
To create a class, you must first create an external ActionScript (AS) file. To create an external AS
file, use your preferred code or text editor.
When you create a class file, you must decide where to store the file and tell Flex how it can find
the file. To tell Flash how to find the file, you must make sure the directory in which you store the
file is in the ActionScript classpath. For more information, see “Understanding the classpath
on page 64.
To create the class file:
1.
Create a directory in your ActionScript classpath, and name it PersonFiles. This directory will
contain all the files for this project.
2.
Create a new file in your preferred text or code editor.
3.
Save the file as Person.as in the PersonFiles directory.