12.1 The Anatomy of an Object

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 12.  Objects and Classes

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.

Figure 12-1 depicts the properties of a sample object called ball. The ball object contains two properties: radius and color. The values of these properties are 50 and 0xFF0000 (the hex value of red). The properties are named with unique identifiers, exactly like variables. Even though each property has its own name, both are contained by the single encompassing object, ball.

Figure 12-1. A sample object structure
figs/act2.1201.gif

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).

Traditionally, object properties are set via methods of the object to preserve the encapsulated nature of an object. That is, in strict OOP, an object should set its own properties. If an outside entity wants to set an object's property, it should do so by calling an appropriate method of the object. For example, a purist would frown on setting the _width property of a movie clip directly. The purist would argue that it is best to let the movie clip object set the _width property itself, and that code outside of the object should do so only indirectly, by calling one of the movie clip object's methods (say, setWidth( )). By moderating access to properties with method calls, the maintainer of the MovieClip class could change the name of the _width property to w without adversely affecting other users of the class.

     



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net