ClassFactory实例.
FLEX的例子中经常看到这个类.来个实例看看如何应用。
来自:http://sherifabdou.com/2008/06/05/example-of-using-the-classfactory-class/
package
{
import flash.display.Graphics;
import mx.core.UIComponent;
public class Square extends UIComponent
{
public var color:uint=0xFFFFFF;
public function Square()
{
super();
}
protected override function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth,unscaledHeight);
var g:Graphics = graphics;
g.clear();
g.beginFill(color);
g.drawRect(0,0,100,100);
g.endFill();
}
}
}
{
import flash.display.Graphics;
import mx.core.UIComponent;
public class Square extends UIComponent
{
public var color:uint=0xFFFFFF;
public function Square()
{
super();
}
protected override function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth,unscaledHeight);
var g:Graphics = graphics;
g.clear();
g.beginFill(color);
g.drawRect(0,0,100,100);
g.endFill();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="initApp()"
layout="horizontal">
<mx:Script>
<![CDATA[
private var myClassFactory:ClassFactory = new ClassFactory(Square);
private function initApp():void
{
var whiteSquare:Square = myClassFactory.newInstance();
myClassFactory.properties={color:0x000000};
var blackSquare:Square = myClassFactory.newInstance();
myClassFactory.properties={color:0xFF0000};
var redSquare:Square = myClassFactory.newInstance();
addChild(whiteSquare);
addChild(blackSquare);
addChild(redSquare);
}
]]>
</mx:Script>
</mx:Application>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="initApp()"
layout="horizontal">
<mx:Script>
<![CDATA[
private var myClassFactory:ClassFactory = new ClassFactory(Square);
private function initApp():void
{
var whiteSquare:Square = myClassFactory.newInstance();
myClassFactory.properties={color:0x000000};
var blackSquare:Square = myClassFactory.newInstance();
myClassFactory.properties={color:0xFF0000};
var redSquare:Square = myClassFactory.newInstance();
addChild(whiteSquare);
addChild(blackSquare);
addChild(redSquare);
}
]]>
</mx:Script>
</mx:Application>
来自:http://sherifabdou.com/2008/06/05/example-of-using-the-classfactory-class/