
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
244
|
Chapter 9: Packages
Defining Packages
Defining a package for a class requires two general steps:
1. Create a directory structure whose name identically matches the package
hierarchy.
2. Specify the package name when defining the class.
Note that there is no such thing as a package file, per se. A package is a concept
based on placing a given class’s .as file in the appropriate folder.
For example, to place the class Player in the package com.yourdomain.game, we’d
follow these steps:
1. Create a new directory named com.
2. Create a new directory inside com named yourdomain.
3. Create a new directory inside yourdomain named game.
4. Create a text file, Player.as, in the directory /com/yourdomain/game/.
5. In Player.as, add the following code:
class com.yourdomain.game.Player {
// Class body not shown...
}
The class declaration must use the fully qualified class name, in this
case com.yourdomain.game.Player, indicating to the compiler that the
package for the Player class is com.yourdomain.game.
If any part of the package or class name does not match the actual names of the sys-
tem directories and .as file, the compiler generates an error (case sensitivity mat-
ters!). For example, if the letter “e” were missing in the word “game” in our class
definition:
// Oops! Missing the "e" in "game"! ...