268 Classes
2. Create the required directory structure after you’ve chosen a package name.
For example, if your package was named
com.macromedia.utils, you would need to
create a directory structure of com/macromedia/utils and place your classes in the
utils folder.
3. Use the com.macromedia.utils prefix for any class you create in this package.
For example, if your class name was ClassA, the full class name would need to be
com.macromedia.utils.ClassA within the com/macromedia/utils/ClassA.as
class file.
4. If you change your package structure at a future point, remember to modify not only the
directory structure, but the package name within each class file, as well as every import
statement or reference to a class within that package.
To continue writing the class files, see “Writing the constructor function” on page 268.
Writing the constructor function
You have already learned how to write the class declaration in “Creating and packaging your
class files” on page 266. In this part of the chapter, you write what’s called the class file’s
constructor function.
Constructors are functions that you use to initialize (define) the properties and methods of a
class. By definition, constructors are functions within a class definition that have the same
name as the class. For example, the following code defines a Person class and implements a
constructor function. In OOP, the constructor function initializes each new instance of
a class.
A class’s constructor is a special function that is called automatically when you create an
instance of a class using the
new operator. The constructor function has the same name as the
class that contains it. For example, the Person class you created contained the following
constructor function:
// Person class constructor function
public function Person (uname:String, age:Number) {
this.__name = uname;
this.__age = age;
}
NOTE
You learn how to write the comments, statements, and declarations in later sections.