User Guide

260 Chapter 10: Creating Custom Classes with ActionScript 2.0
Now, instances of the Person class can add and access properties and methods that arent defined
in the original class, as shown in the following example:
var a_person:Person2 = new Person2();
a_person.hairColor = "blue";//no compiler error because class is dynamic
trace(a_person.hairColor);
Subclasses of dynamic classes are also dynamic, with one exception. Subclasses of the built-in
MovieClip class are not dynamic by default, even though the MovieClip class itself is dynamic.
This implementation provides you with more control over subclasses of the MovieClip class,
because you can choose to make your subclasses dynamic or not:
class A extends MovieClip {} // A is not dynamic
dynamic class B extends A {} // B is dynamic
class C extends B {} // C is dynamic
class D extends A {} // D is not dynamic
dynamic class E extends MovieClip{} // E is dynamic
The following built-in classes are dynamic: Array, ContextMenu, ContextMenuItem, Function,
LoadVars, LocalConnection, MovieClip, SharedObject and TextField.
Using packages
When you are creating classes, organize your ActionScript class files in packages. A package is a
directory that contains one or more class files and that resides in a designated classpath directory
(see “Understanding the classpath” on page 268). A package can, in turn, contain other packages,
called subpackages, each with its own class files.
Package names must be identifiers; that is, the first character must be a letter, underscore (
_), or
dollar sign (
$), and each subsequent character must be a letter, number, underscore, or dollar sign.
Packages are commonly used to organize related classes. For example, you might have three
related classes, Square, Circle, and Triangle, that are defined in Square.as, Circle.as, and
Triangle.as. Assume that youve saved the AS files to a directory specified in the classpath, as
shown in the following example:
// In Square.as:
class Square {}
// In Circle.as:
class Circle {}
// In Triangle.as:
class Triangle {}
Because these three class files are related, you might decide to put them in a package (directory)
called Shapes. In this case, the fully qualified class name would contain the package path, as well
as the simple class name. Package paths are denoted with dot (.) syntax, where each dot indicates
a subdirectory.
For example, if you placed each AS file that defines a shape in the Shapes directory, you would
need to change the name of each class file to reflect the new location, as follows:
// In Shapes/Square.as:
class Shapes.Square {}