About packages

When you are creating classes, you 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 About importing class files and About setting and modifying the classpath). A package can, in turn, contain other packages, called subpackages, each with its own class files.

Like variables, package names must be identifiers; that is, the first character can be a letter, underscore (_), or dollar sign ($), and each subsequent character can be a letter, number, underscore, or dollar sign. There are preferred ways to name packages, which for example recommend that you avoid using underscores or dollar sign characters. For more information on naming packages, see Naming packages.

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 you've saved the ActionScript 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 ActionScript 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 {}

// In Shapes/Circle.as:
class Shapes.Circle {}

// In Shapes/Triangle.as:
class Shapes.Triangle {}

To reference a class that resides in a package directory, you can either specify its fully qualified class name or import the package by using the import statement. For more information, see Working with packages.


Flash CS3


 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000767.html