46
Chapter 2: Creating Custom Classes with ActionScript 2.0
•
“Encapsulation” on page 47
•
“Polymorphism” on page 47
Objects
Think of a real-world object, such as a cat. A cat could be said to have properties (or states), such
as name, age, and color; a cat also has behaviors such as sleeping, eating, and purring. In the world
of object-oriented programming, objects also have properties and behaviors. Using object-
oriented techniques, you can model a real-world object (such as a cat) or a more abstract object
(such as a chemical process).
Note:
The word “behaviors” is used generically here and does not refer to the Behaviors
development panel in the Macromedia Flash interface.
Classes and class members
Continuing with the real-world analogy, consider that there are cats of different colors, ages, and
names, with different ways of eating and purring. But despite their individual differences, all cats
are members of the same category, or in object-oriented programming terms, the same class: the
class of
cats
. In object-oriented programming terminology, each individual cat is said to be an
instance of the cat class.
Likewise, in object-oriented programming, a
class
defines a blueprint for a type of object. The
characteristics and behaviors that belong to a class are jointly referred to as
members
of that class.
The characteristics (in the cat example; name, age, and color) are called
properties
of the class and
are represented as variables; the behaviors (eating, sleeping) are called
methods
of the class and are
represented as functions.
In ActionScript, you define a class with the
class
statement (see
“Creating and using classes”
on page 51
).
Inheritance
One of the primary benefits of object-oriented programming is that you can create
subclasses
of a
class; the subclass then
inherits
all the properties and methods of the
superclass.
The subclass
typically defines additional methods and properties, or
extends
the superclass. Subclasses can also
override
(provide their own definitions for) methods inherited from a superclass.
For example, you might create a Mammal class that defines certain properties and behaviors
common to all mammals. You could then create a Cat subclass that extends the Mammal class.
Using subclasses lets you reuse code, so that instead of re-creating all the code common to both
classes you can simply extend an existing class. Another subclass, say, the Siamese class, could
extend the Cat class, and so on. In a complex application, determining how to structure the
hierarchy of your classes is a large part of the design process.
In ActionScript, you use the
extends
keyword to establish inheritance between a class and its
superclass. For more information, see
“Creating subclasses” on page 55
.
Summary of Contents for FLEX-FLEX ACTIONSCRIPT LANGUAGE
Page 1: ...Flex ActionScript Language Reference...
Page 8: ......
Page 66: ...66 Chapter 2 Creating Custom Classes with ActionScript 2 0...
Page 76: ......
Page 133: ...break 133 See also for for in do while while switch case continue throw try catch finally...
Page 135: ...case 135 See also break default strict equality switch...
Page 146: ...146 Chapter 5 ActionScript Core Language Elements See also break continue while...
Page 808: ...808 Chapter 7 ActionScript for Flash...
Page 810: ...810 Appendix A Deprecated Flash 4 operators...
Page 815: ...Other keys 815 Num Lock 144 186 187 _ 189 191 192 219 220 221 222 Key Key code...
Page 816: ...816 Appendix B Keyboard Keys and Key Code Values...
Page 822: ...822 Index...