EasyManua.ls Logo

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH - Page 272

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
830 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
272 Classes
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.
To initialize properties inline:
1. Open ClassA.as and ClassB.as in the Flash authoring tool.
2. Modify the ClassA class file so the code matches the following ActionScript (the changes
to make appear in boldface):
class com.macromedia.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”.
3. Modify the ClassB class file and add the inline property, changing the value to “ClassB”.
4. Save both ActionScript files before you proceed.
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).
To continue writing your class file, see “Controlling member access in your classes
on page 273.
NOTE
When you initialize arrays inline, only one array is created for all instances of the class.

Table of Contents

Related product manuals