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 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/xyzzycorporation/ package in your classpath. To create an
instance of that class, you could specify the fully qualified class name, as shown in the following
example:
var dataInstance = new com.xyzzycorporation.Data();
You can also use the fully qualified class name to type your variables, as shown in the following
example:
var dataInstance:com.xyzzycorporation.Data = new Data();
You can use the
statement to import packages into a script, which lets you use a class's
import
abbreviated name rather than its fully qualified name. You can also use the wildcard character (*)
to import all the classes in a package.
For example, suppose you created a class named UserClass that's included in the package directory
path com/xyzzycorporation/util/users:
// In the file com/xyzzycorporation/util/users/UserClass.as
class com.xyzzycorporation.util.users.UserClass { ... }
Suppose that in another script, you imported that class using the
statement, as shown in
import
the following example:
import com.xyzzycorporation.util.users.UserClass;
Later, in the same script, you could reference that class by its abbreviated name, as shown in the
following example:
var myUser:UserClass = new UserClass();
You can use the wildcard character (*) to import all the classes in a given package. For example,
suppose you have a package named
that contains two
com.xyzzycorporation.util
ActionScript class files, Rosencrantz.as and Guildenstern.as. In another script, you could import
both classes in that package using the wildcard character, as shown in the following code:
import com.xyzzycorporation.util.*;
The following example shows that you can then reference either of the classes directly in the same
script:
var myRos:Rosencrantz = new Rosencrantz();
var myGuil:Guildenstern = new Guildenstern();
Importing classes
65
Need help?
Do you have a question about the FLEX-FLEX ACTIONSCRIPT LANGUAGE and is the answer not in the manual?