ActionScript 3 Language Specification |
|||
| ActionScript 3.0 Language Specification > 1 Tutorial introduction > 1.9 Namespaces | |||
Namespaces are useful for controlling the visibility of a set of properties independent of the major structure of the program. Packages, classes and interfaces, along with their implicitly defined access control namespaces, allow authors to control the visibility of names in parallel with the organization of those packages, classes and interfaces. But it is sometimes necessary to control the names independent of the lexical structure of a program. Examples of this include the following:
Use packages to give or gain access to a set of features. Use namespaces to give or gain access to a particular facet, version, or privilege independent of the structure of a program. An example that uses namespaces follows:
// ActionScript file: actors/English.as
package actors{
public namespace English = "http://www.adobe.com/2007/Examples/English";
}
// ActionScript file: actors/French.as
package actors {
public namespace French = "http://www.adobe.com/2007/Examples/French";
}
// ActionScript file: actors/BilingualGreeter.as
package actors {
public class BilingualGreeter {
English function hello():void {
trace("hello, world");
}
French function hello():void {
trace("bonjour, le monde");
}
}
}
// ActionScript file: Test.as
package {
import flash.display.Sprite;
public class Test extends Sprite
{
public function Test()
{
}
}
}
import actors.*;
var greeter : BilingualGreeter = new BilingualGreeter();
use namespace English; // Make all identifiers in the English namespace
// visible
greeter.hello(); // Invoke the English version
greeter.French::hello(); // Invoke the French version
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/specs/actionscript/3/as3_specification11.html