|
Chapter 12, "Objects and Classes," introduces
the concept of Object Oriented Programming (OOP). Using
all of the ActionScript learned in previous chapters, readers
will learn what objects and classes are, terminology related
to OOP, how to build custom objects, and finally how to
create chains of inheritance between objects.
The Anatomy of an Object
"Like an array, an object is a container of containers.
A typical array holds multiple data values in individual
numbered elements; an object, analogously, holds multiple
data values in individual named properties. A property is,
effectively, a variable defined on an object. In fact, the
Java programming language calls object properties instance
variables or simply variables (everything is an object in
Java, so there is no need to distinguish a variable from
an object property). A method, by comparison, is a function
defined on an object. Practically speaking, then, an object
is nothing more than a collection of related variables and
functions.
"Typically, an object defines properties that are
meaningfully related. More specifically, the properties
of an object should be chosen such that they capture the
characteristics that distinguish one instance of the object
from another. MovieClip objects, for example, have properties
specific to individual movie clips, such as their number
of frames (_totalframes) and position on screen (_x and
_y).
Instantiating Objects
"Although it may sound backward, lets assume
that we have already created an object class. This assumption
isnt too ludicrous, because ActionScript provides
many built-in classes, and the custom classes well
build will behave similarly. Assuming we have an object
class, we have to create a specific object instance (i.e.,
a copy) based on the class. For example, ActionScript provides
the TextField class, but it is up to us to create individual
text fields.
"To create an instance of an object (i.e., to instantiate
the object), we normally use the
new operator with a constructor function, which initializes
the object. The general
syntax is:
new ConstructorFunction()
"Lets instantiate our first object, using a
constructor function thats already built into ActionScript:
the Date() constructor. A Date object represents a particular
point in time. When we instantiate a new object, we normally
store the resulting instance in a variable, an array element,
or a property of another object for later access. For example,
the following code creates a new Date object and stores
a reference to it in the variable named now:
var now = new Date();
"A specific time is a complex set of numbers that
includes a second, minute, hour, day, month, and year. Using
a Date objects methods, we can easily check or set
any aspect of the time represented by the object. For example,
we can invoke the getHours( ) method to check the hour (in
24-hour format), as follows:
trace("The current hour is: "
+ now.getHours( ));
Classes and Object-Oriented Programming
"Its not uncommon to create dozens of complex
objects that store rich information about everything from
products in a shopping-cart system to bad guys with artificial
intelligence in a video game. To expedite the creation of
objects and to define object hierarchies (relationships
between objects), we use object classes. A class
is a template-style definition of an entire category of
objects.
Making a Class
"There is no specific class declaration
device in ActionScript; there is no class statement
that creates a new class as the var statement creates new
variables. Instead, we define a special type of function,
called a constructor function, that generates a new instance
of our class. By defining the constructor function, we are
effectively creating our class template or class definition.
"Syntactically, constructor functions (or simply constructors)
are formed just like normal functions. For example:
function Constructor () {
statements
}
"The name of a classs constructor function can
be any valid function name, but, by convention, it is capitalized
to indicate that it is a class constructor. A constructors
name should describe the class of objects it creates, as
in Ball, Product, or Vector2d. The
statements of a constructor function initialize the objects
it creates."
The rest of Chapter 12 discusses creating methods and
properties of the class, instantiating your custom class,
creating object inheritance, and the built-in objects in
ActionScript. The chapter concludes with an OOP Quick Reference
that summarizes the key points of the chapter and provides
ActionScript templates for custom objects and classes.
You can download the entire version of Chapter
12 below:
|