
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Methods
|
109
In fact, getter and setter method usage can even be nested. For example, we could
change the preceding getArea( ) method definition into a getter method definition as
follows:
public function get area ( ):Number {
return width * height;
}
In that case, we could use pseudo-properties (width, height, and area) to assign a
Box instance a height and width, and even to retrieve its area:
var b:Box = new Box( );
b.width = 20;
b.height = 10;
trace(b.area); // Displays: 200
Simulating read-only properties
A getter/setter pseudo-property can be made read-only by declaring a getter without
declaring a setter.
For example, the
area property that we just defined does not have a setter because its
value depends on the values of other properties. It doesn’t make sense for a user of
the class to set the area directly, so we disable assignment by not defining a setter.
However, to explain this limitation to users who attempt to set the
area property, we
could define a do-nothing setter that prints a debug message to the Output panel, as
follows:
function set area ( ):Void {
trace("Warning: The area property cannot be set directly.");
}
Don’t confuse read-only pseudo-properties with private properties (or, for that mat-
ter, public properties). A read-only pseudo-property can be read from outside