Creating And Using Classes - MACROMEDIA FLEX-FLEX ACTIONSCRIPT LANGUAGE Reference

Actionscript language reference
Table of Contents

Advertisement

This code invokes the Person class's constructor function, passing as parameters the values
and 32.
The
newPerson
compiler to ensure that you don't try to access properties or methods that aren't defined in the
class. See
"Strict data typing" on page
using the
dynamic
To create an instance of the Person class:
Create a new file in your preferred text or code editor.
1.
Save the file as createPerson.as in the PersonFiles directory you created.
2.
Enter the following code:
3.
var person_1:Person = new Person("Nate", 32);
var person_2:Person = new Person("Jane", 28);
trace(person_1.getInfo());
trace(person_2.getInfo());
This code creates two instances of the Person class,
method on each instance.
getInfo()
You should now have a basic understanding of how to create and use classes in your scripts. The
rest of this chapter discusses classes and interfaces in more detail.

Creating and using classes

As discussed in
"Using classes: a simple example" on page
declaration and the body. The class declaration consists minimally of the
followed by an identifier for the class name, then left and right curly braces ({}). Everything inside
the braces is the class body, as shown in the following example:
class className {
// class body
}
You can define classes only in AS files. Class names must be identifiers—that is, the first character
must be a letter, underscore (
number, underscore, or dollar sign. The class name must exactly match the name of the AS file
that contains it, including capitalization. In the following example, if you create a class called
Shape, the AS file that contains the class definition must be named Shape.as:
// In file Shape.as
class Shape {
// Shape class body
}
All AS class files that you create must be saved in one of the designated classpath directories—
directories where Flash looks for class definitions when compiling scripts—that is, in the same
directory where the FLA file that refers to the class is stored. For more information on including
AS files in Flex applications, see "Using ActionScript" in Developing Flex Applications.
variable is typed as a Person object. Typing your objects in this way enables the
24. (The exception is if you declare the class to be dynamic
keyword. See
"Creating dynamic classes" on page
), or dollar sign (
_
and
person_1
person_2
48, a class consists of two parts: the
), and each subsequent character must be a letter,
$
Creating and using classes
"Nate"
56.)
, and then calls the
statement,
class
51

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the FLEX-FLEX ACTIONSCRIPT LANGUAGE and is the answer not in the manual?

Subscribe to Our Youtube Channel

This manual is also suitable for:

Flex

Table of Contents