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.
 | 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. |
Next: Inheritance, Packages, Compilation >>
More Flash Articles
More By O'Reilly Media