Packages
Like its name suggests, a package is a conceptual container for a group of classes
and, as we’ll learn later, for other things in a program. Each package delimits an
independent physical region of a program and gives that region a name, called the
package name. By convention, package names typically start with a lowercase letter
while class names typically start with an uppercase letter. This helps distinguish
package names from class names.
When a class’s source code resides within a package, that class automatically adopts
the package’s name as part of its own name, much like a child takes on his parents’
family name. For example, a class named Player in a package named game becomes
known as game.Player. Notice that the package name comes first and is separated
from the class name using a period (.) character (character is simply programming
jargon for letter, number, punctuation, and so on). The package name helps distinguish
the game.Player class from other classes also named Player, thus preventing
name conflicts between different parts of a program or between a program’s custom
classes and ActionScript’s built-in classes.
To create a new package, we use a package definition directive. Let’s dissect that
term. In ActionScript, all program instructions are known generally as directives.
Definitions are one type of directive; they create, or define something, such as a package
or a class. In this case, the thing being defined is a package, hence the term, package
definition directive.
A definition that creates something in a program is said to define or
declare that thing. Definitions are sometimes also referred to as
declarations.
Here’s the general form of a package definition directive:
package packageName {
}
All package definitions start with a keyword: package. A keyword is a command
name reserved for use by the ActionScript language. In this case, the package
keyword tells ActionScript to create a package. After the package keyword, we provide
the desired package name, represented by packageName in the preceding code.
(Throughout this book, italicized code, such as packageName, indicates text that must
be replaced by the programmer.) Next, we mark the beginning and end of the package
contents using curly braces: { and }. To add a class to a package, we insert its
source code between the curly braces, as follows:
package packageName {
Class source code goes here
}
10 | Chapter 1: Core Concepts