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
.
NOT
E
When you initialize arrays inline, only one array is created for all instances of the class.
Содержание FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Страница 1: ...Learning ActionScript 2 0 in Flash...
Страница 8: ...8 Contents...
Страница 18: ...18 Introduction...
Страница 30: ...30 What s New in Flash 8 ActionScript...
Страница 66: ...66 Writing and Editing ActionScript 2 0...
Страница 328: ...328 Interfaces...
Страница 350: ...350 Handling Events...
Страница 590: ...590 Creating Interaction with ActionScript...
Страница 710: ...710 Understanding Security...
Страница 730: ...730 Debugging Applications...
Страница 780: ...780 Deprecated Flash 4 operators...
Страница 830: ...830 Index...