Macromedia flex 2 Manual
30
Using ActionScript to Create Components
Defining properties as variables
Properties let you define data storage within your class. You can define your properties as
public, which means that they can be accessed by users of the class. You can also define
properties as private, which means that they are used internally by the class, as the following
example shows:
public, which means that they can be accessed by users of the class. You can also define
properties as private, which means that they are used internally by the class, as the following
example shows:
public class MyButton extends Button {
// Define private vars.
private var currentFontSize:Number;
private var currentFontSize:Number;
// Define public vars.
public var maxFontSize:Number = 15;
public var minFontSize:Number = 5;
public var maxFontSize:Number = 15;
public var minFontSize:Number = 5;
}
Users of the class can access the public variables but not the private variables, as the following
example shows:
example shows:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:MyComp="myControls.*">
<MyComp:MyButton label="Submit" maxFontSize="30"/>
</mx:Application>
Although you can define your classes to use public properties, you may find it advantageous to
define properties by using setter and getter methods. For more information, see
define properties by using setter and getter methods. For more information, see
Defining properties as getters and setters
You can define properties for your components by using setter and getter methods. The
advantage of getters and setters is that they isolate the variable from direct public access so that
you can perform the following actions:
advantage of getters and setters is that they isolate the variable from direct public access so that
you can perform the following actions:
■
Inspect and validate any data written to the property on a write
■
Trigger events that are associated with the property when the property changes
■
Calculate a return value on a read
■
Allow a child class to override
NO
T
E
You cannot override an inherited property defined by a variable, but you can override a
property defined by setter and getter methods. You can reset the value of an inherited
property defined by a variable. You typically reset it in the constructor of the subclass for
an ActionScript component, or in an event handler for an MXML component because
MXML components cannot define a constructor.
property defined by setter and getter methods. You can reset the value of an inherited
property defined by a variable. You typically reset it in the constructor of the subclass for
an ActionScript component, or in an event handler for an MXML component because
MXML components cannot define a constructor.