For example, you might start with a class called Mammal that has
You then create Cat, Monkey, and Dog subclasses to extend the Mammal class. The subclasses
override the
play()
of animals. Monkey implements the
method to pounce at a ball of yarn; Dog implements the
play()
Because the
sleep()
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:
Defining a class in an ActionScript class file.
1.
Saving the class file to the directory that is in the ActionScript classpath (a location where Flex
2.
looks for classes).
Creating an instance of the class in another script, or creating a subclass based on the original
3.
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
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
on page
64.
To create the class file:
Create a directory in your ActionScript classpath, and name it PersonFiles. This directory will
1.
contain all the files for this project.
Create a new file in your preferred text or code editor.
2.
Save the file as Person.as in the PersonFiles directory.
3.
48
Chapter 2: Creating Custom Classes with ActionScript 2.0
method from the Mammal class, to reflect the habits of those particular kinds
play()
functionality is similar between the animals, you would use the superclass
"Creating and using interfaces" on page
play()
method to swing from trees; Cat implements the
play()
"Understanding the classpath"
and
methods.
sleep()
method to fetch a ball.
58.
Need help?
Do you have a question about the FLEX-FLEX ACTIONSCRIPT LANGUAGE and is the answer not in the manual?