Adobe FLEX 2-PROGRAMMING ACTIONSCRIPT 3.0 Manual page 39

Programming actionscript 3.0
Table of Contents

Advertisement

Many developers, especially those with Java programming backgrounds, may choose to place
only classes at the top level of a package. ActionScript 3.0, however, supports not only classes
at the top level of a package, but also variables, functions, and even statements. One advanced
use of this feature is to define a namespace at the top level of a package so that it will be
available to all classes in that package. Note, however, that only two access specifiers,
and
, are allowed at the top level of a package. Unlike Java, which allows you to
internal
declare nested classes private, ActionScript 3.0 supports neither nested nor private classes.
In many other ways, however, ActionScript 3.0 packages are similar to packages in the Java
programming language. As you can see in the previous example, fully qualified package
references are expressed using the dot operator (
packages to organize your code into an intuitive hierarchical structure for use by other
programmers. This facilitates code sharing by allowing you to create your own package to
share with others, and to use packages created by others in your code.
The use of packages also helps to ensure that the identifier names that you use are unique and
do not conflict with other identifier names. In fact, some would argue that this is the primary
benefit of packages. For example, two programmers who wish to share their code with each
other may have each created a class called SampleCode. Without packages, this would create a
name conflict, and the only resolution would be to rename one of the classes. With packages,
however, the name conflict is easily avoided by placing one, or preferably both, of the classes
in packages with unique names.
You can also include embedded dots in your package name to create nested packages. This
allows you to create a hierarchical organization of packages. A good example of this is the
flash.xml package provided by the Flash Player API. The flash.xml package is nested inside the
flash package.
The flash.xml package contains the legacy XML parser that was used in previous versions of
ActionScript. One reason that it now resides in the flash.xml package is that the name of the
legacy XML class conflicts with the name of the new XML class that implements the XML for
ECMAScript (E4X) specification functionality available in ActionScript 3.0.
Although moving the legacy XML class into a package is a good first step, most users of the
legacy XML classes will import the flash.xml package, which will generate the same name
conflict unless you remember to always use the fully qualified name of the legacy XML class
(flash.xml.XML). To avoid this situation, the legacy XML class is now named
XMLDocument, as the following example shows:
package flash.xml
{
class XMLDocument {}
class XMLNode {}
class XMLSocket {}
}
), just as they are in Java. You can use
.
Packages and namespaces
public
39

Advertisement

Table of Contents
loading

This manual is also suitable for:

Flex

Table of Contents