这段代码来自Adobe docs:
package {
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.*;
import flash.net.URLRequest;
public class LoaderExample extends Sprite {
private var url:String = "Image.gif";
public function LoaderExample() {
var loader:Loader = new Loader();
configureListeners(loader.contentLoaderInfo);
loader.addEventListener(MouseEvent.CLICK, clickHandler);
var request:URLRequest = new URLRequest(url);
loader.load(request);
addChild(loader);
}
private function configureListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.COMPLETE, completeHandler);
dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
dispatcher.addEventListener(Event.INIT, initHandler);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
dispatcher.addEventListener(Event.OPEN, openHandler);
dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
dispatcher.addEventListener(Event.UNLOAD, unLoadHandler);
}
private function completeHandler(event:Event):void {
trace("completeHandler: " + event);
}
//...
}
有人能告诉我为什么loader.contenLoaderInfo(作为configureListeners的参数传递)是IEventDispatcher(而不是LoaderInfo)对象吗?
答案 0 :(得分:1)
LoaderInfo扩展了EventDispatcher,后者又实现了IEventDispatcher接口。我不认为有这样的理由,除了它是发送事件调度程序的最通用方式。
这意味着您可以将函数更改为接受LoaderInfo,它将完全相同,但如果您希望将该函数用于调度事件的其他函数,则不能通用。