Macromedia flex 2 Manuale

Pagina di 254
Adding properties and methods to a component
125
Adding properties and methods to a 
component
To make your custom components reusable, you design them so that users can pass 
information to them. This section describes how to add public properties and methods to 
your components. It also describes how the component user can call the methods and access 
the properties, and how to make them accessible in MXML. 
Defining public properties in ActionScript
You can use one of the following methods to add public properties to your ActionScript 
components:
Define public variables 
Define public getter and setter methods
Accessing public properties in MXML
All public properties defined in your component are accessible in MXML by using MXML 
tag properties. For example, you might allow the user to pass a value to your component, as 
the following example shows:
<MyComp:MyCustomComponent prop1="3"/>
To create a component that takes tag attributes in MXML, you define a public variable with 
the same name as the tag attribute in your class definition:
public class MyCustomComponent extends TextArea {
// Define an uninitialized variable.
public var prop1:Number;
// Define and initialize a variable.
public var prop2:Number=5;
...
}
You can also use public getter and setter methods to define a property, as the following 
example shows:
public class MyCustomComponent extends TextArea {
private var _prop1:Number;
public function get prop1():Number {
// Method body.
// Typically the last line returns the value of the private variable.