我之前没有使用过implements关键字,我一直在尝试使用它来实现IEventDispatcher类,看看是否允许我addEventListener()在扩展的类中使用Object(这是我对它的理解是什么 - 如果我是正确的我错了.
我的班级是这样的:
package
{
import flash.events.Event;
import flash.events.IEventDispatcher;
public class Thing extends Object implements IEventDispatcher
{
public function Thing()
{
addEventListener(Event.ENTER_FRAME, _handle);
}
private function _handle(e:Event):void
{
trace('a');
}
}
}
Run Code Online (Sandbox Code Playgroud)
但这会引发一堆错误:
1180:调用可能未定义的方法addEventListener.
1044: 接口方法addEventListener在命名空间flash.events:IEventDispatcher中未由类Thing实现.
1044:接口方法removeEventListener在命名空间flash.events:IEventDispatcher中没有由类Thing实现.
1044:接口方法dispatchEvent在命名空间flash.events:IEventDispatcher中没有由类Thing实现.
1044:接口方法hasEventListener在命名空间flash.events:IEventDispatcher中没有由类Thing实现.
1044:接口方法willTrigger在命名空间flash.events:IEventDispatcher中没有由类Thing实现.
我哪里出错了?
为了添加jeremymealbrown的答案,Adobe 在文档中提供了一个示例:
import flash.events.IEventDispatcher;
import flash.events.EventDispatcher;
import flash.events.Event;
class DecoratedDispatcher implements IEventDispatcher {
private var dispatcher:EventDispatcher;
public function DecoratedDispatcher() {
dispatcher = new EventDispatcher(this);
}
public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void{
dispatcher.addEventListener(type, listener, useCapture, priority);
}
public function dispatchEvent(evt:Event):Boolean{
return dispatcher.dispatchEvent(evt);
}
public function hasEventListener(type:String):Boolean{
return dispatcher.hasEventListener(type);
}
public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void{
dispatcher.removeEventListener(type, listener, useCapture);
}
public function willTrigger(type:String):Boolean {
return dispatcher.willTrigger(type);
}
}
Run Code Online (Sandbox Code Playgroud)