60
Chapter 2: Creating Custom Classes with ActionScript 2.0
For example, the following code first checks if the object name
newBox
implements the Movable
interface before calling the
moveUp()
method on the object:
if (Movable(newBox) != null) {
newBox.moveUp();
}
For more information about casting, see
“Casting objects” on page 25
.
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,
picture_01
, an instance of a custom class, invokes the
showInfo()
method:
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
sqrt()
method of the Math class:
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
age
member variable of the Person class is an instance
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
age
property would not be a good class member, because each person has a different 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
species
member that indicates the proper Latin
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
"Homo sapiens"
for every
instance of the class, so this member should be a class member.
Содержание FLEX
Страница 1: ...Flex ActionScript Language Reference ...
Страница 8: ......
Страница 66: ...66 Chapter 2 Creating Custom Classes with ActionScript 2 0 ...
Страница 76: ......
Страница 133: ...break 133 See also for for in do while while switch case continue throw try catch finally ...
Страница 135: ...case 135 See also break default strict equality switch ...
Страница 146: ...146 Chapter 5 ActionScript Core Language Elements See also break continue while ...
Страница 229: ...while 229 i 3 The following result is written to the log file 0 3 6 9 12 15 18 See also do while continue for for in ...
Страница 808: ...808 Chapter 7 ActionScript for Flash ...
Страница 810: ...810 Appendix A Deprecated Flash 4 operators ...
Страница 815: ...Other keys 815 Num Lock 144 186 187 _ 189 191 192 219 220 221 222 Key Key code ...
Страница 816: ...816 Appendix B Keyboard Keys and Key Code Values ...
Страница 822: ...822 Index ...