270 Classes
5. Save both ActionScript files before you proceed.
To continue writing your class file, see “Adding methods and properties” on page 270.
Adding methods and properties
To create the properties for the ClassA and ClassB classes, use the var keyword to define
variables.
To add properties to the ClassA and ClassB classes:
1. Open ClassA.as and ClassB.as in the Flash authoring tool.
2. Modify the ClassA.as ActionScript file to match the following code (the changes to make
appear in boldface):
class com.macromedia.utils.ClassA {
static var _className:String;
function ClassA() {
trace("ClassA constructor");
}
}
The previous block of code adds a single new static variable, _className, which contains
the name of the current class.
3. Modify the ClassB class and add the static variable so it is similar to the previous code.
4. Save both ActionScript files before you proceed.
You use the post-colon syntax (for example,
var username:String and var age:Number) in
the variable declarations. This is an example of strict data typing. When you type a variable
using the
var variableName:variableType format, the ActionScript compiler ensures that
any values assigned to that variable match the specified type. If the correct data type is not
used in the FLA file importing this class, the compiler throws an error. For more information
on strict data typing, see “About assigning data types and strict data typing” on page 81.
NOTE
The following three exercises are part of “Example: Writing custom classes”
on page 263. If you do not wish to progress through the example, you can download the
class files from www.helpexamples.com/flash/learnas/classes/.
TIP
By convention, class properties are defined at the top of the class body. Defining
them at the top makes the code easier to understand, but isn’t required.