Organizing Classes in Packages

 <  Day Day Up  >  

In ActionScript 2.0, classes and interfaces can be organized using packages. This allows for cleaner organization and management of large-scale projects. Packages are used by ActionScript 2.0 at both compile time and at runtime. At compile time, packages are simply the hierarchy of folders that contain the class definitions. In the following code, at compile time, mx.data represents subfolders in a directory where the Product.as file is stored. At runtime, mx and data are objects that are created.

 cClass mx.data.Product { //code } 

In the preceding example, the Product class (Product.as) would be located in the subfolder mx and then the subfolder data. Remember that class definitions must always be stored in a separate ActionScript file. Thus, for a frame script to reference this class, you would need to import the class or explicitly spell out the path somewhere in the code, as in the following example:

 var myProduct:Product = new mx.data.Product(); 

It is also possible to explicitly import the class using the import keyword, as in the following example:

 import mx.data.Product; 

Importing a class and referencing the path is acceptable and does not generate any errors. You can also import classes, as shown in Figure 1.6.

Figure 1.6. Using publish settings to allow easy importing of multiple classes from one package.

graphics/01fig06.jpg

At runtime, packages exist as Object containers and can be referenced in that manner. For example, in the following code, mx and data are both objects:

 class mx.data.Product { //code } 
 <  Day Day Up  >  


Object-Oriented Programming with ActionScript 2.0
Object-Oriented Programming with ActionScript 2.0
ISBN: 0735713804
EAN: 2147483647
Year: 2004
Pages: 162

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net