Interfaces are a type of class that you design to act as an outline for your components. When you write an interface, you provide only the names of public methods rather than any implementation. For example, if you define two methods in an interface and then implement that interface, the implementing class must provide implementations of those two methods.
Interfaces in ActionScript can declare methods and properties only by using setter and getter methods; they cannot specify constants. The benefit of interfaces is that you can define a contract that all classes that implement that interface must follow. Also, if your class implements an interface, instances of that class can also be cast to that interface.
Custom MXML components can implement interfaces just as other ActionScript classes can. To do this, you use the implements attribute. All MXML tags support this attribute.
The following code is an example of a simple interface that declares several methods:
// The following is in a file named SuperBox.as.
interface SuperBox {
function selectSuperItem():String;
function removeSuperItem():Boolean;
function addSuperItem():Boolean;
}
A class that implements the SuperBox interface uses the implements attribute to point to its interface and must provide an implementation of the methods. The following example of a custom ComboBox component implements the SuperBox interface:
<?xml version="1.0"?>
<!-- StateComboBox.mxml -->
<mx:ComboBox xmlns:mx="http://www.adobe.com/2006/mxml" implements="SuperBox">
<mx:Script>
<![CDATA[
public function selectSuperItem():String {
return "Super Item was selected";
}
public function removeSuperItem():Boolean {
return true;
}
public function addSuperItem():Boolean {
return true;
}
]]>
</mx:Script>
<mx:dataProvider>
<mx:String>AK</mx:String>
<mx:String>AL</mx:String>
</mx:dataProvider>
</mx:ComboBox>
You can implement multiple interfaces by separating them with commas, as the following example shows:
<mx:ComboBox xmlns:mx="http://www.adobe.com/2006/mxml" implements="SuperBox, SuperBorder, SuperData">
All methods that you declare in an interface are considered public. If you define an interface and then implement that interface, but do not implement all of its methods, the MXML compiler throws an error.
Methods that are implemented in the custom component must have the same return type as their corresponding methods in the interface. If no return type is specified in the interface, the implementing methods can declare any return type.
You cannot define a constructor for an MXML component. If you do, the Flex compiler issues an error message that specifies that you defined a duplicate function.
For many types of Flex components, you can use an event listener instead of a constructor. For example, depending on what you want to do, you can write an event listener for the preinitialize, initialize, or creationComplete event to replace the constructor.
These events are all defined by the UIComponent class, and inherited by all of its subclasses. If you create an MXML component that is not a subclass of UIComponent, you cannot take advantage of these events. You can instead implement the IMXMLObject interface in your MXML component, and then implement the IMXMLObject.initialized() method, as the following example shows:
<?xml version="1.0"?>
<!-- mxmlAdvanced/myComponents/ObjectComp.mxml -->
<mx:Object xmlns:mx="http://www.adobe.com/2006/mxml"
implements="mx.core.IMXMLObject">
<mx:Script>
<![CDATA[
// Implement the IMXMLObject.initialized() method.
public function initialized(document:Object, id:String):void {
trace("initialized, x = " + x);
}
]]>
</mx:Script>
<mx:Number id="y"/>
<mx:Number id="z"/>
<mx:Number id="x"/>
</mx:Object>
Flex calls the IMXMLObject.initialized() method after it initializes the properties of the component. The following example uses this component:
<?xml version="1.0"?>
<!-- mxmlAdvanced/MainInitObject.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:MyComp="myComponents.*"
creationComplete="initApp();">
<mx:Script>
<![CDATA[
public function initApp():void {
myTA.text="myFC.x = " + String(myFC.x);
}
]]>
</mx:Script>
<MyComp:ObjectComp id="myFC" x="1" y="2" z="3"/>
<mx:TextArea id="myTA"/>
</mx:Application>
The executing SWF file for the previous example is shown below:
Because Flex calls the IMXMLObject.initialized() method after it initializes the properties of the component, the trace() function in the implementation of the IMXMLObject.initialized() method outputs the following:
initialized, x = 1