If you are creating multiple custom classes, use packages to organize your class files. A package is a
directory that contains one or more class files and resides in a designated classpath directory. Class
names must be fully qualified within the file in which it is declared—that is, it must reflect the
directory (package) in which it is stored.
For example, a class named myClasses.education.curriculum.RequiredClass is stored in the
myClasses/education/curriculum package. The class declaration in the RequiredClass.as file looks
like this:
class myClasses.education.curriculum.RequiredClass {
}
For this reason, it's good practice to plan your package structure before you begin creating classes.
Otherwise, if you decide to move class files after you create them, you will have to modify the
class declaration statements to reflect their new location. For more information on organizing
classes, see
"Using packages" on page
For more information on creating and using classes, see the following topics:
•
"Constructor functions" on page 52
•
"Creating properties and methods" on page 53
•
"Controlling member access" on page 53
•
"Initializing properties inline" on page 54
•
"Creating subclasses" on page 55
Constructor functions
A class's constructor is a special function that is called automatically when you create an instance
of a class using the
contains it. For example, the Person class you created contained the following constructor
function:
// Person class constructor function
function Person (myName:String, myAge:Number) {
this.name = myName;
this.age = myAge;
}
If no constructor function is explicitly declared—that is, if you don't create a function whose
name matches that of the class—the compiler automatically creates an empty constructor
function for you.
A class can contain only one constructor function; overloaded constructor functions are not
allowed in ActionScript 2.0.
A constructor function should have no return type.
The
keyword is not required in ActionScript 2.0 class definitions because the compiler
this
resolves the reference and adds it into the bytecode. However, using
readability..
52
Chapter 2: Creating Custom Classes with ActionScript 2.0
57.
operator. The constructor function has the same name as the class that
new
can improve your code's
this
Need help?
Do you have a question about the FLEX-FLEX ACTIONSCRIPT LANGUAGE and is the answer not in the manual?