MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT Reference Manual page 159

Actionscript reference guide
Hide thumbs Also See for FLASH MX 2004 - ACTIONSCRIPT:
Table of Contents

Advertisement

Next you'll create the
6
values of the
and
age
body, as shown below.
class Person {
var age:Number;
var name:String;
// Method to return property values
function showInfo():String {
return("Hello, my name is " + name + " and I'm " + age + " years old.");
}
}
Notice the use of data typing (optional but recommended) in the function definition.
function showInfo():String {...}
In this case, what's being typed is the
The last bit of code you'll add in this section is a special function called a constructor function.
7
In object-oriented 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;
// Method to return property values
function showInfo():String {
return("Hello, my name is " + name + " and I'm " + age + " years old.");
}
// Constructor function
function Person (myName:String, myAge:Number) {
name = myName;
age = myAge;
}
}
The
constructor function takes two parameters,
Person()
those parameters to the
typed as String and Number, respectively. For more information about constructor functions,
see
"Constructor functions" on page
Note: If you don't create a constructor function, an empty one is created automatically
during compilation.
Save the file as Person.as in the PersonFiles directory that you created in step 1.
8
If you're using Flash MX 2004 (not Flash Professional), proceed to the next section.
(Flash Professional only) Check the syntax of the class file by selecting Tools > Check Syntax,
9
or pressing Control+T (Windows) or Command+T (Macintosh).
If any errors are reported in the Output panel, compare the code in your script to the final
code in step 7, above. If you can't fix the code errors, copy the completed code in step 7 from
the Help panel.
method, which returns a preformatted string containing the
showInfo()
properties. Add the
name
showInfo()
and
properties. The two function parameters are strictly
name
age
163.
function definition to the class
showInfo()
function's return value (a string).
and
myName
Using classes: a simple example
, and assigns
myAge
159

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the FLASH MX 2004 - ACTIONSCRIPT and is the answer not in the manual?

Table of Contents