注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

XINBO'S SAP BLOG

你所浪费的今天,是昨天死去的人奢望的明天。你所厌恶的现在,是未来的你回不去的曾经

 
 
 

日志

 
 

abap object-oriented–使用事件  

2009-05-18 12:53:11|  分类: OO |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

这一节我们将参照一个例子,介绍事件(event)的使用方法。

1.定义event

2.Set handler

3.Raise event

REPORT zbobo_events_5.
*---------------------------------------------------------------------*
*       CLASS lcl_dog DEFINITION
*---------------------------------------------------------------------*
CLASS lcl_dog DEFINITION.
  PUBLIC SECTION.
*   Declare events
    EVENTS:
      dog_is_hungry
        EXPORTING value(ex_time_since_last_meal) TYPE i.
    METHODS:
      constructor
          IMPORTING im_name TYPE string,
      set_time_since_last_meal
          IMPORTING im_time TYPE i,
      on_dog_is_hungry FOR EVENT dog_is_hungry OF lcl_dog
          IMPORTING ex_time_since_last_meal.
ENDCLASS.
*---------------------------------------------------------------------*
*       CLASS lcl_dog IMPLEMENTATION
*---------------------------------------------------------------------*
CLASS lcl_dog IMPLEMENTATION.
  METHOD constructor.
    WRITE: / 'I am a dog and my name is', im_name.
  ENDMETHOD.
  METHOD set_time_since_last_meal.
    IF im_time < 4.
      SKIP 1.
      WRITE: / 'You fool, I am not hungry yet'.
    ELSE.
*    Subsrcribe for event:
*    set handler <Event handler method>
*    FOR <ref_sender>!FOR ALL INSTANCES [ACTIVATION <var>]
      SET HANDLER on_dog_is_hungry FOR ALL INSTANCES ACTIVATION 'X'.
*    Raise event
      RAISE EVENT dog_is_hungry
        EXPORTING ex_time_since_last_meal = im_time.
    ENDIF.
  ENDMETHOD.
  METHOD on_dog_is_hungry.
*   Event method, called when the event dog_is_hungry is raised
    SKIP 1.
    WRITE: /  'You son of a bitch. I have not eaten for more than',
              ex_time_since_last_meal, ' hours'.
    WRITE: / 'Give me something to eat NOW!'.
  ENDMETHOD.
ENDCLASS.
*---------------------------------------------------------------------*
*       R E P O R T
*---------------------------------------------------------------------*
DATA: o_dog1 TYPE REF TO lcl_dog.
START-OF-SELECTION.
  CREATE OBJECT o_dog1 EXPORTING im_name = 'Beefeater'.
  CALL METHOD o_dog1->set_time_since_last_meal
    EXPORTING im_time = 2.
* This method call will raise the event dog_is_hungy
* because time > 3
  CALL METHOD o_dog1->set_time_since_last_meal
    EXPORTING im_time = 5.

  评论这张
 
阅读(375)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017