Home arrow Flash arrow Page 4 - Object-oriented ActionScript
FLASH

Object-oriented ActionScript


This chapter provides a high-level overview of object-oriented programming (OOP) in ActionScript for experienced programmers who are making their first foray into Flash development. (From the book Essential ActionScript 2.0, by Colin Moock, O'Reilly Media, ISBN:0596006527.)

Author Info:
By: O'Reilly Media
Rating: 4 stars4 stars4 stars4 stars4 stars / 15
August 30, 2004
TABLE OF CONTENTS:
  1. · Object-oriented ActionScript
  2. · Key Object-Oriented Programming Concepts
  3. · Object Creation and Usage
  4. · Encapsulation and Datatypes
  5. · Inheritance, Packages, Compilation
  6. · Starting an Objected-Oriented Application
  7. · An Example Website

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Object-oriented ActionScript - Encapsulation and Datatypes
(Page 4 of 7 )

Objects are said to encapsulate their property values and method source code from the rest of the program. If properly designed, an object’s private properties and the internal code used in its methods (including public methods) are its own business; they can change without necessitating changes in the rest of the program. As long as the method names (and their parameters and return values) stay the same, the rest of the program can continue to use the object without being rewritten.

Encapsulation is an important aspect of object-oriented design because it allows different programmers to workon different classes independently. As long as they agree on the names of the public methods through which they’ll communicate, the classes can be developed independently. Furthermore, by developing a specification that shows the publicly available methods, the parameters they require, and the values they return, a class can be tested thoroughly before being deployed. The same test code can be used to reverify the class’s operation even if the code within the class is refactored (i.e., rewritten to enhance performance or to simplify the source code without changing the previously existing functionality).

In Chapter 4, we’ll learn how to use the private modifier to prevent a method or property from being accessed by other parts of a program.

Datatypes

Each class in an object-oriented program can be thought of as defining a unique kind of data, which is formally represented as a datatype in the program.

Note: A class effectively defines a custom datatype.

You are probably already familiar with custom datatypes defined by built-in Action-Script classes, such as the Date class. That is, when you create a Date object using new Date( ), the returned value contains not a string or a number but a complex datatype that defines a particular day of a particular year. As such, the Date datatype supports various properties and methods uniquely associated with dates.

Datatypes are used to impose limits on what can be stored in a variable, used as a parameter, or passed as a return value. For example, when we defined the speed property earlier, we also specified its datatype as Number (as shown in bold):

// The expression ":Number" defines speed's datatype.
public var speed:Number;

Attempts to store a nonnumeric value in the speed property generate a compile-time error.

If you test a movie and Flash’s Output panel displays an error containing the phrase “Type mismatch,” you know that you used the wrong kind of data somewhere in your program (the compiler will tell you precisely where). Datatypes help us guarantee that a program isn’t used in unintended ways. For example, by specifying that the datatype of speed is a number, we prevent someone from unintentionally setting speed to, say, the string “very fast.” The following code generates a compile-time error due to the datatype mismatch:

public var speed:Number = "very fast"; // Error!
                        // You can't assign a String to a
                        // variable whose type is Number.

We’ll talk more about datatypes and type mismatches in Chapter 3.

Buy the book!If you've enjoyed what you've seen here, or to get more information, click on the "Buy the book!" graphic. Pick up a copy today!

Visit the O'Reilly Network http://www.oreillynet.com for more online content.


blog comments powered by Disqus
FLASH ARTICLES

- More Top Flash Game Tutorials
- Top Flash Game Tutorials
- Best Flash Photo Gallery Tutorials
- The Top Flash Tutorials for Menus
- 7 Great Flash Tutorials
- Adobe Creative Suite 5.5 Now Available
- Critical Flash Vulnerability Heats Up the Web
- More on Nonpersistent Client-Side Remote Sha...
- Nonpersistent Client-Side Remote Shared Obje...
- Using the Decorator Pattern for a Real Web S...
- Using Concrete Decorator Classes
- Delving More Deeply into the Decorator Patte...
- The Decorator Pattern in Action
- A Simple Decorator Pattern Example
- Decorator Pattern

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials