The Objects Shall Inherit the Earth

 <  Day Day Up  >  

Now that you know how to define a class, you have to learn how to use it. A class definition is used to create an object instance at runtime. Figure 2.3 shows the class definition and its transformation to instances.

Figure 2.3. Class definition and instances.

graphics/02fig03.gif

Just as an architectural blueprint is used to create the actual physical house, so too is a class definition used to create an instance of a class.

Creating Objects from Classes

When you compile an FLA into an SWF, the ActionScript compiler adds all the class definitions needed to the SWF. This includes any built-in or core classes, such as MovieClip , as well as all your custom classes that have either been referenced or explicitly included. For the compiler to find the classes, however, you may have to take additional steps, such as modifying the class path or explicitly including the file containing the class definition.

include Versus import

To include a file containing ActionScript explicitly in a Flash document, you can use the #include directive. This includes the contents of the specified file, as if the commands in the file were part of the Flash document itself. The #include directive is invoked at compile time. Therefore, if you make any changes to an external file, you must recompile any FLA files and redeploy. In Listing 2.8, you can see how to access files from the current directory, as well as from other directories.

Listing 2.8. #include Directive Variants
 #include "utilityMethods.as" #include "includeDir/utilityMethods.as" #include "../includeDir/utilityMethods.as" 

N OTE

Notice that there is no semicolon terminating the statements in Listing 2.8. If you include a semicolon, the compiler generates an error.


The #include directive is used to include ActionScript 2.0 code that exists in an external file ”such as a set of utility methods or initialization variables . The #include directive can appear in a Flash document on the timeline or in an external ActionScript 2.0 file except when the external ActionScript 2.0 file is a class definition. The class file counterpart for #include is import .

The import directive enables you to import the definition of a class, thus making it available within the including class. import is an OOP standard for referencing classes within classes. Most OOP languages have this construct. The import directive tells the compiler to incorporate the imported class when building the .swf file.

You can use #include in FLAs to include unstructured code or class definitions. You can also use it in .as files that are not class definitions to include unstructured code or class definitions.

Note that import is an OOP construct that it is used in class files to include other class definitions.

Packages and the Classpath

We can import an entire group of classes. To do this, you organize your class files in packages. A package is a directory that contains one or more class files and that resides in a designated classpath directory. A package can, in turn , contain other packages, called subpackages , each with its own class files.

To locate the external ActionScript 2.0 files that contain the class definitions, Flash searches the classpath. Classes should be saved to one of the directories specified in the classpath, or a subdirectory therein. Otherwise, Flash cannot resolve , or locate, the class specified in the script. The subdirectories created within a classpath directory define the packages. You can change the classpath for all Flash files through the Preferences panel, as shown in Figure 2.4.

Figure 2.4. Modifying the global classpath for all Flash files.

graphics/02fig04.gif

The classpath can also be set for a particular document through the Publish Settings panel, as shown in Figure 2.5.

Figure 2.5. Modifying the classpath for a single document.

graphics/02fig05.gif

As mentioned, packages are commonly used to organize related classes. For example, you might have three related classes, Loan , Customer , and Calculator , that are defined in Loan.as, Customer.as, and Calculator.as, respectively. They are saved to a directory called LoanPackage.

There are two ways that you can reference the Customer class. If the calling file is in a different directory, you can use a qualified path, as in Listing 2.9. This allows the compiler to find the class at compile time regardless of where it is stored on the local file system.

Listing 2.9. Using a Fully Qualified Classpath
 class Loan(){       ...       var customer:LoanPackage.Customer;       ... } 

The path uses dot notation just as you would use the "/" in a directory structure.

You can also import the entire package into the calling class and reference it directly, as in Listing 2.10. The wildcard * is used to include the entire contents of the directory.

Listing 2.10. Using an Import Statement to Import an Entire Package
 import LoanPackage.*; class Loan(){       ...       var customer:Customer;       ... } 

The import statement can import an entire directory, as shown in Listing 2.10, or you can specifically import only one class, as shown in Listing 2.11.

Listing 2.11. Using an Import Statement to Import a Single Class
 import LoanPackage.Customer; class Loan(){       ...       var customer:Customer;       ... } 

So far, you have managed to make the class definition accessible in your class, but you haven't created an instance. To create an instance, you have to introduce the new keyword. The new keyword is used to invoke the constructor of a class and create an instance in memory. If you recall, the constructor of a class always has the same name as the class. Listing 2.12 creates an instance of the Customer class as a class property within the Loan class.

Listing 2.12. Creating an Instance of a Class by Calling the Constructor
 import LoanPackage.Customer; class Loan(){       ...  var customer:Customer = new Customer();  ... } 

You can create an instance of the Loan class inside a user interface called loanCalc.fla . To create an instance of the Loan class, you use the following syntax with the new keyword to invoke the constructor:

 var loanInstance:Loan; loanInstance = new Loan(); 

The variable loanInstance now has a reference to the Loan object instance in memory, including all of its properties and methods. Figure 2.6 diagrams the process of generating an instance in memory at runtime.

Figure 2.6. Generating an instance in memory at runtime.

graphics/02fig06.jpg

In Flash, you can create class files in the same environment as you create FLA files. A class file has the same name as the class itself, and it has the extension .as. Thus, your Loan class will be in a file called Loan.as.

In Figure 2.7, the Loan.as file is in the same directory as LoanCalc.fla; therefore, you do not need an explicit include directive. The compiler finds the class file using the classpath. In Figure 2.4, you might have noticed a classpath entry of ' . ' ( a single period). This tells the compiler to look in the current working directory.

Figure 2.7. Using the Loan class from LoanCalc.fla within the development environment in Flash.

graphics/02fig07.gif

Notice that in Figure 2.7 on line 3, we are creating the reference variable of type Loan called loanInstance , while on line 4, we are creating the instance in memory.

 <  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