ABAP Keyword Documentation → ABAP − Reference → Declarations → Declaration Statements → Classes and Interfaces → ABAP Objects - Overview → Examples for ABAP Objects
ABAP Objects, Events
This example demonstrates how events are raised and handled.
Other versions: 7.31 | 7.40 | 7.54
Source Code
REPORT demo_abap_objects_events NO STANDARD PAGE HEADING.
<span class="blue">************************************************************************</span>
<span class="blue">* Declarations</span>
<span class="blue">************************************************************************</span>
INTERFACE vehicle.
DATA
max_speed TYPE i.
EVENTS
speed_change EXPORTING
VALUE(id) TYPE i
VALUE(new_speed) TYPE i.
METHODS:
drive IMPORTING id TYPE i,
stop IMPORTING id TYPE i.
ENDINTERFACE.
CLASS ship DEFINITION.
PUBLIC SECTION.
METHODS
constructor.
INTERFACES
vehicle.
PRIVATE SECTION.
ALIASES
max FOR vehicle~max_speed.
DATA
ship_speed TYPE i.
ENDCLASS.
CLASS truck DEFINITION.
PUBLIC SECTION.
METHODS
constructor.
INTERFACES
vehicle.
PRIVATE SECTION.
ALIASES
max FOR vehicle~max_speed.
DATA
truck_speed TYPE i.
ENDCLASS.
CLASS status DEFINITION.
PUBLIC SECTION.
CLASS-EVENTS
button_clicked
EXPORTING VALUE(fcode) TYPE sy-ucomm.
CLASS-METHODS:
class_constructor,
user_action.
ENDCLASS.
CLASS list DEFINITION.
PUBLIC SECTION.
METHODS:
fcode_handler
FOR EVENT button_clicked OF status
IMPORTING fcode,
list_change
FOR EVENT speed_change OF vehicle
IMPORTING id
new_speed,
list_output.
PRIVATE SECTION.
TYPES:
BEGIN OF line,
iref TYPE REF TO vehicle,
speed TYPE i,
END OF line.
DATA
list TYPE standard TABLE OF line WITH empty key.
ENDCLASS.
<span class="blue">************************************************************************</span>
<span class="blue">* Implementations</span>
<span class="blue">************************************************************************</span>
CLASS ship IMPLEMENTATION.
METHOD constructor.
max = 30.
ENDMETHOD.
METHOD vehicle~drive.
CHECK ship_speed < max.
ship_speed += 10.
RAISE EVENT vehicle~speed_change
EXPORTING id = id
new_speed = ship_speed.
ENDMETHOD.
METHOD vehicle~stop.
CHECK ship_speed > 0.
ship_speed = 0.
RAISE EVENT vehicle~speed_change
EXPORTING id = id
new_speed = ship_speed.
ENDMETHOD.
ENDCLASS.
CLASS truck IMPLEMENTATION.
METHOD constructor.
max = 100.
ENDMETHOD.
METHOD vehicle~drive.
CHECK truck_speed < max.
truck_speed += 50.
RAISE EVENT vehicle~speed_change
EXPORTING id = id
new_speed = truck_speed.
ENDMETHOD.
METHOD vehicle~stop.
CHECK truck_speed > 0.
truck_speed = 0.
RAISE EVENT vehicle~speed_change
EXPORTING id = id
new_speed = truck_speed.
ENDMETHOD.
ENDCLASS.
CLASS status IMPLEMENTATION.
METHOD class_constructor.
SET PF-STATUS 'VEHICLE'.
WRITE 'Click a button!'.
ENDMETHOD.
METHOD user_action.
RAISE EVENT button_clicked EXPORTING fcode = sy-ucomm.
ENDMETHOD.
ENDCLASS.
CLASS list IMPLEMENTATION.
METHOD fcode_handler.
CASE fcode.
WHEN 'CREA_SHIP'.
list = VALUE #( BASE list
( iref = NEW ship( ) ) ).
WHEN 'CREA_TRUCK'.
list = VALUE #( BASE list
( iref = NEW truck( ) ) ).
WHEN 'DRIVE'.
CHECK sy-lilli > 0.
list[ sy-lilli ]-iref->drive( sy-lilli ).
WHEN 'STOP'.
LOOP AT list ASSIGNING FIELD-SYMBOL(<line>).
data(id) = sy-tabix.
<line>-iref->stop( id ).
ENDLOOP.
WHEN 'CANCEL'.
LEAVE PROGRAM.
ENDCASE.
list_output( ).
ENDMETHOD.
METHOD list_change.
list[ id ]-speed = new_speed.
ENDMETHOD.
METHOD list_output.
sy-lsind = 0.
SET TITLEBAR 'TIT'.
LOOP AT list ASSIGNING FIELD-SYMBOL(<line>).
IF <line>-iref is INSTANCE OF ship.
WRITE / icon_ws_ship AS ICON.
ELSEIF <line>-iref is INSTANCE OF truck.
WRITE / icon_ws_truck AS ICON.
ENDIF.
WRITE: 'Speed = ', <line>-speed.
ENDLOOP.
ENDMETHOD.
ENDCLASS.
<span class="blue">************************************************************************</span>
<span class="blue">* Program events</span>
<span class="blue">************************************************************************</span>
START-OF-SELECTION.
DATA(list) = NEW list( ).
SET HANDLER:
list->fcode_handler,
list->list_change FOR ALL INSTANCES.
AT USER-COMMAND.
status=>user_action( ).
Description
The user actions on a classic list raise events in ABAP Objects. The list and its data are implemented
in the class list
. The class status
is used to
process user actions. It raises an event button_clicked
at AT
USER-COMMAND time. The event is handled in the class list
. The class
list
contains an object of the class ship
or
truck for each line of the list. Both classes implement the interface vehicle
.
If the speed of one of these objects changes, the event speed_change
is raised. The class list
responds to this event by modifying the list output.