
Importing classes
171
Using packages
You can 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 169
.) 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 you’ve saved the AS files to a directory specified in the classpath.
// 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 {}
// 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 (see below).
Importing classes
To reference a class in another script, you must prefix the class name with the class’s package path.
The combination of a class’s name and its package path is the class’s
fully qualified class name
. If a
class resides in a top-level classpath directory—not in a subdirectory in the classpath directory—
then its fully qualified class name is just its class name.
To specify package paths, use dot notation to separate package directory names. Package paths are
hierarchical, where each dot represents a nested directory. For example, suppose you create a class
named Data that resides in a com/network/ package in your classpath. To create an instance of
that class, you could specify the fully qualified class name, as follows:
var dataInstance = new com.network.Data();
Содержание FLASH MX 2004 - ACTIONSCRIPT
Страница 1: ...ActionScript Reference Guide...
Страница 8: ...8 Contents...
Страница 12: ......
Страница 24: ...24 Chapter 1 What s New in Flash MX 2004 ActionScript...
Страница 54: ...54 Chapter 2 ActionScript Basics...
Страница 80: ...80 Chapter 3 Writing and Debugging Scripts...
Страница 82: ......
Страница 110: ...110 Chapter 5 Creating Interaction with ActionScript...
Страница 112: ......
Страница 120: ...120 Chapter 6 Using the Built In Classes...
Страница 176: ......
Страница 192: ...192 Chapter 10 Working with External Data...
Страница 202: ...202 Chapter 11 Working with External Media...
Страница 204: ......
Страница 782: ...782 Chapter 12 ActionScript Dictionary...
Страница 793: ...Other keys 793 221 222 Key Key code...
Страница 794: ...794 Appendix C Keyboard Keys and Key Code Values...
Страница 798: ...798 Appendix D Writing Scripts for Earlier Versions of Flash Player...
Страница 806: ...806 Appendix E Object Oriented Programming with ActionScript 1...
Страница 816: ...816 Index...