MACROMEDIA FLEX-FLEX ACTIONSCRIPT LANGUAGE Reference page 50

Actionscript language reference
Table of Contents

Advertisement

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
7.
values of the
age
after the constructor function, as shown in 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;
}
// Method to return property values
function getInfo():String {
return("Hello, my name is " + this.name + " and I'm " + this.age + "
years old.");
}
}
This code is the completed code for this class. The return value of the
strictly typed (optional, but recommended) as a string.
Save the file.
8.
You've successfully created a class file. To continued with this example, see
of the Person class" on page
Creating an instance of the Person class
The next step is to create an instance of the Person class in another script and assign it to a
variable. To create an instance of a custom class, you use the
when creating an instance of a built-in ActionScript class (such as the Date or Error class). You
refer to the class using its fully qualified class name or import the class; see
on page
65.
Continuing the example you started in
in the same directory as the class file you created, you can refer to the class file using the fully
qualified class name, which is
Person class and assigns it to the variable
var newPerson:Person = new Person("Nate", 32);
50
Chapter 2: Creating Custom Classes with ActionScript 2.0
constructor function takes two parameters,
and
name
age
52.
method, which returns a preformatted string containing the
getInfo()
and
properties. Add the
name
50.
"Creating a class file" on page
. For example, the following code creates an instance of the
Person
myName
properties. The two function parameters are strictly
function definition to the class body,
getInfo()
new
:
newPerson
and
, and assigns
myAge
function is
getInfo()
"Creating an instance
operator, the same as you would
"Importing classes"
48, if you create a FLA file

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the FLEX-FLEX ACTIONSCRIPT LANGUAGE and is the answer not in the manual?

This manual is also suitable for:

Flex

Table of Contents