You can use the import
statement to allow "short cut" references to single classes, or to packages containing multiple classes using wildcards. The compiler keeps track of all classes that have been imported using the import
statement and substitutes the full class name for any abbreviated references found. This occurs anywhere where an identifier is expected.
For example, suppose you created a class named UserClass that's included in the package macr.util.users:
// In the file macr/util/users/UserClass.as class macr.util.users.UserClass { ... }
In another script, you could use the import
statement to import the reference to that class, as shown below.
import macr.util.users.UserClass;
Later in the script you could reference that class by its simple name.
var myUser:UserClass = new UserClass();
If an imported class is not used in the following script, the class is not included in the resulting SWF's bytecode. This is consistent with how the import
statement works in the Java programming language.
The import
statement applies only to the current script (frame or object) in which it's called. For instance, if you import a class on a script on frame 1 of a Flash document, you can only reference the class on that same frame, not on another frame.
You can use the wildcard character (*) to import all the classes in a given package. For example, say you have a package named macr.util
that contains two ActionScript class files, foo.as and bar. as. In another script, you could import both classes in that package using the wildcard character, as shown below.
import macr.util.*;
In the same script, you can then reference either the foo or bar classes directly.
var myFoo:foo = new foo(); var myBar:bar = new bar();