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


     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 }

  • 相关阅读:
    个人管理:提高你的搜商
    敏捷个人:提供更多文档下载,并转载一篇敏捷个人读书笔记
    个人管理: 激励你的一句话
    敏捷个人 从Scrum实践来思考如何导入价值观
    信息系统开发平台OpenExpressApp 如何解决ComboBox.TextProperty绑定带来问题的来龙去脉
    敏捷个人 敏捷个人价值观,欢迎提出你的意见和你的价值观
    使用VS2010的CodedUI来做自己的自动化测试框架
    .Net4下的MEF(Managed Extensibility Framework) 架构简介
    IronRuby - 快速在半小时学习Ruby基础知识
    敏捷个人 项目网站文档页签增加blog链接
  • 原文地址:https://www.cnblogs.com/zack/p/1439421.html
Copyright © 2020-2023  润新知