Instance And Class Members - MACROMEDIA FLEX-FLEX ACTIONSCRIPT LANGUAGE Reference

Actionscript language reference
Table of Contents

Advertisement

For example, the following code first checks if the object name
interface before calling the
if (Movable(newBox) != null) {
newBox.moveUp();
}
For more information about casting, see

Instance and class members

In object-oriented programming, members (properties or methods) of a class can be
instance members or class members. Instance members are created for each instance of the class;
they are defined to the prototype of the class when they are declared outside of its constructor
function. In contrast, class members are created once per class. (Class members are also known as
static members.)
To invoke an instance method or access an instance property, you reference an instance of the
class. In the following example,
method:
showInfo()
picture_01.showInfo();
Class (static) members, however, are assigned to the class, not to any instance of the class. To
invoke a class method or access a class property, you reference the class name, rather than a
specific instance of the class, as shown in the following example:
ClassName.classMember;
For example, the ActionScript Math class consists only of static methods and properties. To call
any of its methods, you don't create an instance of the Math class. Instead, you simply call the
methods on the Math class itself. The following code calls the
var square_root:Number = Math.sqrt(4);
Creating class members
All the members (methods and properties) discussed so far in this chapter are of a type called
instance members. For each instance member, there's a unique copy of that member in every
instance of the class. For example, the
member, because each Person has a different age.
Another type of member is a class member. There is only one copy of a class member, which is
used for the entire class.
The
property would not be a good class member, because each person has a different age.
age
Only properties and methods that are shared by all individuals of the class should be class
members.
Suppose that you want every class to have a
name for the species that the class represents. For every Person object, the species is Homo
sapiens. It would be wasteful to store a unique copy of the string
instance of the class, so this member should be a class member.
60
Chapter 2: Creating Custom Classes with ActionScript 2.0
method on the object:
moveUp()
"Casting objects" on page
, an instance of a custom class, invokes the
picture_01
member variable of the Person class is an instance
age
species
implements the Movable
newBox
25.
method of the Math class:
sqrt()
member that indicates the proper Latin
"Homo sapiens"
for every

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