Brief Introduction to OOP
Object-oriented programming (OOP) is sometimes
shrouded in mystery and mysticism. You might have
encountered
phrases such as "the zen of OOP." But when you get right down to
it, OOP is just a way of organizing code. I'd even go so far as to
say that it's the best way to organize complex or large
projects.
Chapter 11, "Introducing ActionScript," covered
simple datatypes in Flash.
Variables
are containers, and datatypes
are a way to say what kind of data can be stored in that container.
A variable with a number datatype holds a number, a string holds a
string of
characters
, and an array holds a list of strings,
numbers
, or objects.
Each datatype has certain properties and methods
unique to its type. For example, a number can hold numerical values
and have mathematical
methods
applied to it. A class defines a
datatype.
When we start discussing objects, things start
getting more interesting. An object can hold properties, which are
simply variables of a datatype (the object container holds the
variable container). Objects can also hold another complex datatype
called
functions
. In Chapter 11,
functions were defined as encapsulated segments of code with a
single set of functionality. They are "run" when called by
name
in
another section of code. When functions are stored in an object,
they are called
methods
.
Classes
So how do you organize code differently when you
work with ActionScript 2.0 and object-oriented programming in
Flash? When Chapter 11 introduced ActionScripting, you learned to
look at variables and functions as ways to organize your code. Now
you're ready to move to a more sophisticated organizational
structure where all of your code is stored in objects, which are
simply containers of containers.
However, before you can create an instance of an
object, you must have a class. A
class
describes an object as well as what
properties and methods belong to that object. It can be one of the
built-in classes, such as Math, or a custom class that you write
yourself. In either case, a class is a datatype that describes all
the properties and methods that are available to be applied to an
object created from the class description.
You can think of a class as the blueprint for an
object. Just as many
buildings
can be
constructed
from the same set
of blueprints, multiple objects can be described by a single class.
It is important to understand that
all
datatypes and objects in Flash are
class-based. The
MovieClip
object,
Date
object,
Array
object,
Math
object, and even the
Object
object are all based on classes that are built into
Flash. For a complete list of all built-in datatypes (classes)
available to you in Flash, go to the Action panel and choose Types
from the left Action listing.
|