Example: Writing custom classes
269
Consider the following points when you write constructor functions:
■
If no constructor function is explicitly declared—that is, if you don’t create a function
whose name matches that of the class—the compiler automatically creates an empty
constructor function for you.
■
A class can contain only one constructor function; overloaded constructor functions are
not allowed in ActionScript 2.0.
■
A constructor function should have no return type.
The term
constructor
is also typically used when you create (instantiate) an object based on a
particular class. The following statements are calls to the constructor functions for the top-
level Array class and the custom Person class:
var day_array:Array = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
"Sat");
var somePerson:Person = new Person("Tom", 30);
Next you’ll add a special function called a constructor function.
To add the constructor functions to your class files:
1.
Open the ClassA.as class file in the Flash authoring tool.
2.
Modify the existing class file so it matches the following code (the changes to make appear
in boldface):
class com.macromedia.utils.ClassA {
function ClassA() {
trace("ClassA constructor");
}
}
The previous code defines a constructor method for the ClassA class. This constructor
traces a simple string to the Output panel, which will let you know when a new instance
of the class has been created.
3.
Open the ClassB.as class file in the Flash authoring tool.
4.
Modify the class file so it matches the following code (the changes to make appear
in boldface):
class com.macromedia.utils.ClassB {
function ClassB() {
trace("ClassB constructor");
}
}
NO
TE
The following exercise is 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/
.
Содержание 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...