To create the properties for the Person class, use the
6.
and
age
name
class Person {
var age:Number;
var name:String;
}
Tip: By convention, class properties are defined at the top of the class body, which makes the
code easier to understand, but this isn't required.
The colon syntax (
is an example of strict data typing. When you type a variable in this way (
variableName:variableType
assigned to that variable match the specified type. If the correct data type is not used in the
FLA file importing this class, an error is thrown by the compiler. Using strict typing is good
practice and can make debugging your scripts easier. (For more information, see
typing" on page
Next, you'll add a special function called a constructor function. In object-oriented
7.
programming, the constructor function initializes each new instance of a class.
The constructor function always has the same name as the class. To create the class's
constructor function, add the following code:
class Person {
var age:Number;
var name:String;
// Constructor function
function Person (myName:String, myAge:Number) {
this.name = myName;
this.age = myAge;
}
}
The
Person()
those parameters to the
typed as String and Number, respectively. Unlike other functions, the constructor function
should not declare its return type. For more information about constructor functions, see
"Constructor functions" on page
If you don't create a constructor function, an empty one is created automatically
during compilation.
Last, you'll create the
8.
values of the
age
after the constructor function, as shown in the following code:
class Person {
var age:Number;
var name:String;
252
Chapter 10: Creating Custom Classes with ActionScript 2.0
, as shown in the following example:
var age:Number
), the ActionScript 2.0 compiler ensures that any values
41.)
constructor function takes two parameters,
and
name
age
255.
method, which returns a preformatted string containing the
getInfo()
and
properties. Add the
name
keyword to define two variables named
var
and
var name:String
properties. The two function parameters are strictly
function definition to the class body,
getInfo()
) used in the variable declarations
var
"Strict data
and
, and assigns
myName
myAge
Need help?
Do you have a question about the FLASH MX 2004-USING ACTIONSCRIPT IN FLASH and is the answer not in the manual?
Questions and answers