User Guide
Packages and namespaces 43
The compiler has no way of knowing which SampleCode class to use. To resolve this conflict,
you must use the fully qualified name of each class, as follows:
var sample1:samples.SampleCode = new samples.SampleCode();
var sample2:langref.samples.SampleCode = new langref.samples.SampleCode();
Namespaces
Namespaces give you control over the visibility of the properties and methods that you create.
Think of the
public, private, protected, and internal access control specifiers as built-
in namespaces. If these predefined access control specifiers do not suit your needs, you can
create your own namespaces.
If you are familiar with XML namespaces, much of this discussion will not be new to you,
although the syntax and details of the ActionScript implementation are slightly different from
those of XML. If you have never worked with namespaces before, the concept itself is
straightforward, but the implementation has specific terminology that you will need to learn.
To understand how namespaces work, it helps to know that the name of a property or method
always contains two parts: an identifier and a namespace. The identifier is what you generally
think of as a name. For example, the identifiers in the following class definition are
sampleGreeting and sampleFunction():
class SampleCode
{
var sampleGreeting:String;
function sampleFunction () {
trace(sampleGreeting + " from sampleFunction()");
}
}
Whenever definitions are not preceded by a namespace attribute, their names are qualified by
the default
internal namespace, which means they are visible only to callers in the same
package. If the compiler is set to strict mode, the compiler issues a warning that the
internal
namespace applies to any identifier without a namespace attribute. To ensure that an
identifier is available everywhere, you must specifically precede the identifier name with the
public attribute. In the previous example code, both sampleGreeting and
sampleFunction() have a namespace value of internal.
NOTE
Programmers with a C++ background often confuse the import statement with
#include. The #include directive is necessary in C++ because C++ compilers process
one file at a time, and will not look in other files for class definitions unless a header file is
explicitly included. ActionScript 3.0 has an include directive, but it is not designed to
import classes and packages. To import classes or packages in ActionScript 3.0, you
must use the import statement and place the source file that contains the package in
the class path.