FLEX (actionscript3) 设计回调函数范例

it2022-05-05  123

 1  package  inc  2  {  3       public   class  EventNotifier  4      {  5           private  var ie:InterestingEvent;  6                   private  var yesNo:Boolean;  7           public  function EventNotifier(event:InterestingEvent)  8          {  9              ie  =  event; 10                          yesNo  =   false ; 11          } 12           public  function readyDoWork(): void 13          { 14              trace( " 哈哈,设定条件 " ); 15                          trace( " 完成一些任务后,把某个条件设为true " ); 16                          trace( " 这样,doWork()就可以工作了 " ); 17                           this .yesNo  =   true ; 18          } 19           public  function doWork(): void 20          { 21               if  (yesNo) 22                          { 23                               // 通知interestingEvent说,可以做它该做的事情了 24                              ie.interestingEvent(); 25                          } 26              } 27      } 28  }  1  <? xml version="1.0" ?>  2  < mx:Application  xmlns:mx ="http://www.adobe.com/2006/mxml"  xmlns:local ="*"  creationComplete ="init();" >  3       < mx:Script >  4           <![CDATA[  5              import inc.*;  6              private function init():void{  7                  var callme:CallMe=new CallMe();  8                  callme.en.readyDoWork();  9                  callme.en.doWork(); 10                  trace("done~"); 11              } 12           ]]> 13       </ mx:Script > 14  </ mx:Application > 1  package  inc 2  { 3       public   interface  InterestingEvent 4      { 5          function interestingEvent(): void ; 6      } 7  }

 1  package  inc  2  {  3       public   class  CallMe  implements  InterestingEvent  4      {  5           public  var en:EventNotifier;  6           public  function CallMe()  7          {  8                // 创建事件通知对象  9                        // 因为稍候要回调它的interestingEvent()方法,所以要把自身传递出去 10                       en  =   new  EventNotifier ( this ); 11          } 12           13           // 当某件事发生后,要让这个方法做的事情 14           public  function interestingEvent(): void 15          { 16              trace( " 在这里完成一系列需要回调函数完成的任务 " ); 17          }     18      } 19  }

转载于:https://www.cnblogs.com/zack/archive/2009/04/20/1439421.html

相关资源:事件机制 观察者模式

最新回复(0)