ABAP 面向对象系列之七

20075261124301908


这一节我们将参照一个例子,介绍事件(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.


About The Author

从事SAP行业有些年头,对SAP技术稍有了解; 如无特别声明,本博客文章为原创,转载请注明; 博主MSN:xuchunbo0901@hotmail.com; 博主邮箱:bob.xu@abaptech.com。

1 Comment

  1. job offers 说道:

    Hi! I’ve been following your web site for a while now and finally got the courage to go ahead and give you a shout out from Huffman Texas! Just wanted to mention keep up the fantastic work!

Leave A Reply


注意: 评论者允许使用'@user空格'的方式将自己的评论通知另外评论者。例如, ABC是本文的评论者之一,则使用'@ABC '(不包括单引号)将会自动将您的评论发送给ABC。使用'@all ',将会将评论发送给之前所有其它评论者。请务必注意user必须和评论者名相匹配(大小写一致)。

无觅相关文章插件,快速提升流量