Flash CS3 Documentation |
|||
| Learning ActionScript 2.0 in Adobe Flash > Classes > Example: Writing custom classes > Adding methods and properties | |||
To create the properties for the ClassA and ClassB classes, use the var keyword to define variables.
|
NOTE |
|
The following three exercises are part of Example: Writing custom classes. If you do not wish to progress through the example, you can download the class files from www.helpexamples.com/flash/learnas/classes/. |
class com.adobe.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.
|
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. |
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.
A class's members consist of properties (variable declarations) and methods (function definitions). You must declare and define all properties and methods inside the class body (the curly braces [{}]); otherwise, an error occurs during compilation. For information on members, see About public, private, and static methods and properties (members).
class com.adobe.utils.ClassA {
static var _className:String;
function ClassA() {
trace("ClassA constructor");
}
function doSomething():Void {
trace("ClassA - doSomething()");
}
}
The block of code in boldface creates a new method in the class, which traces a string to the Output panel.
If any errors are reported in the Output panel, compare the ActionScript in your script to the complete code written in the previous step. If you cannot fix the code errors, copy and paste the complete code into the Script window before you proceed.
If any errors appear in the Output panel, copy and paste the complete code into the Script window before you proceed:
class com.adobe.utils.ClassB {
static var _className:String;
function ClassB() {
trace("ClassB constructor");
}
function doSomething():Void {
trace("ClassB - doSomething()");
}
}
You can initialize properties inline--that is, when you declare them--with default values, as shown in the following example:
class Person {
var age:Number = 50;
var username:String = "John Doe";
}
When you initialize properties inline, the expression on the right side of an assignment must be a compile-time constant. That is, the expression cannot refer to anything that is set or defined at runtime. Compile-time constants include string literals, numbers, Boolean values, null, and undefined, as well as constructor functions for the following top-level classes: Array, Boolean, Number, Object, and String.
class com.adobe.utils.ClassA {
static var _className:String = "ClassA";
function ClassA() {
trace("ClassA constructor");
}
function doSomething():Void {
trace("ClassA - doSomething()");
}
}
The only difference between the existing class file and the previous block of code is there is now a value defined for the static _className variable, "ClassA".
This rule applies only to instance variables (variables that are copied into each instance of a class), not class variables (variables that belong to the class).
|
NOTE |
|
When you initialize arrays inline, only one array is created for all instances of the class. |
To continue writing your class file, see Controlling member access in your classes.
Flash CS3