ActionScript Syntax for Flex Applications - Declaring Classes
(Page 2 of 4 )
Next, let’s look at the basic syntax and structure of a class. At a minimum, all ActionScript 3.0 classes consist of the following elements:
- Class package declaration
- Class declaration
Additionally, classes almost always also haveimportstatements.
Creating class files
Each class must be defined in its own file. (There are a few unique exceptions, but in most practical cases, a class must be defined in its own file.) The name of the file must be the same as the name of the class it contains, and the file must use the .as file extension. For instance, if you want to define an Example class, you must create a file named Example.as.
Package declarations
The syntax for all ActionScript 3.0 classes begins with a package declaration. As discussed earlier in this chapter, packages are used to organize classes. A package name in ActionScript corresponds to the directory structure within which the ActionScript file is stored. Each directory and subdirectory is delimited by a dot (.) in a package name. For example, if a class is stored in the example subdirectory of a com directory, the package name would becom.example. A class’s package declaration uses thepackagekeyword followed by the package name. Opening and closing curly braces, which contain anyimportstatements and class declarations, follow thepackagedeclaration. The followingpackagedeclaration says that the enclosed class exists within thecom.examplepackage. This also means that the file must exist within a com/example directory relative to one of the source path directories:
package com.example {
// Import statements go here.
// Class declaration goes here.
}
It’s considered a best practice to place all class files within packages with the possible exception of main class files when creating ActionScript 3.0-only (non-Flex) applications.
Import statements
As noted earlier, import statements should appear within thepackage declaration, but not within the class declaration. (Technically,importstatements can be placed anywhere, but by convention, they should be placed within thepackagedeclaration, but not in the class declaration.) You mustimportany and all classes you intend to use.
ActionScript 3.0 classes don’t automatically import classes. The following example imports theURLLoaderandURLRequestclasses from the Flash Player API:
package com.example{
import flash.net.URLLoader;
import flash.net.URLRequest;
// Class declaration goes here.
}
Class declaration
All public ActionScript 3.0 classes placed within package declarations must be declared using thepublickeyword, followed by theclasskeyword and the name of the class. Opening and closing curly braces then follow, within which you place the class definition. Class names always start with initial capital letters by convention. The following example declares anExampleclass in thecom.examplepackage:
package com.example {
import flash.net.URLLoader;
import flash.net.URLRequest;
public class Example {
// Class code goes here.
}
}
Next: Variables and Properties >>
More Flash Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of the book Programming Flex 2, written by Chafic Kazoun and Joey Lott (O'Reilly, 2007; ISBN: 059652689X). Check it out today at your favorite bookstore. Buy this book now.
|
|