EVENT

扫码查看
report demo_abap_objects_events no standard page heading.

************************************************************************
* Declarations
************************************************************************

interface i_vehicle.
  data     max_speed type i.
  events:  speed_change exporting value(new_speedtype i.
  methodsdrive,
           stop.
endinterface.

class c_ship definition.
  public section.
    methods constructor.
    interfaces i_vehicle.
  private section.
    aliases max for i_vehicle~max_speed.
    data ship_speed type i.
endclass.

class c_truck definition.
  public section.
    methods constructor.
    interfaces i_vehicle.
  private section.
    aliases max for i_vehicle~max_speed.
    data truck_speed type i.
endclass.

class status definition.
  public section.
    class-events button_clicked exporting value(fcodetype sy-ucomm.
    class-methodsclass_constructor,
                   user_action.
endclass.

class c_list definition.
  public section.
    methodsfcode_handler for event button_clicked of status
                               importing fcode,
             list_change   for event speed_change of i_vehicle
                               importing new_speed,
             list_output.
  private section.
    dataid type i,
          ref_ship  type ref to c_ship,
          ref_truck type ref to c_truck,
          begin of line,
            id type i,
            flag(1type c,
            iref  type ref to i_vehicle,
            speed type i,
          end of line,
          list like sorted table of line with unique key id.
endclass.

data list type ref to c_list.

************************************************************************
* Implementations
************************************************************************

class c_ship implementation.
  method constructor.
    max 30.
  endmethod.
  method i_vehicle~drive.
    check ship_speed max
.
    ship_speed ship_speed + 10.
    raise event i_vehicle~speed_change
                exporting new_speed ship_speed.
  endmethod.
  method i_vehicle~stop.
    check ship_speed > 0.
    ship_speed 0.
    raise event i_vehicle~speed_change
                exporting new_speed ship_speed.
  endmethod.
endclass.

class c_truck implementation.
  method constructor.
    max 150.
  endmethod.
  method i_vehicle~drive.
    check truck_speed max.
    truck_speed truck_speed + 50.
    raise event i_vehicle~speed_change
                exporting new_speed truck_speed.
  endmethod.
  method i_vehicle~stop.
    check truck_speed > 0.
    truck_speed 0.
    raise event i_vehicle~speed_change
                exporting 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 c_list implementation.
  method fcode_handler.
    clear line.
    case fcode.
      when 'CREA_SHIP'.
        id id 1.
        create object ref_ship.
        line-id id.
        line-flag 'C'.
        line-iref ref_ship.
        append line to list.
      when 'CREA_TRUCK'.
        id id 1.
        create object ref_truck.
        line-id id.
        line-flag 'T'.
        line-iref ref_truck.
        append line to list.
      when 'DRIVE'.
        check sy-lilli > 0.
        read table list index sy-lilli into line.
        line-iref->drive).
      when 'STOP'.
        loop at list into line.
          line-iref->stop).
        endloop.
      when 'CANCEL'.
        leave program.
    endcase.
    list_output).
  endmethod.
  method list_change.
    line-speed new_speed.
    modify table list from line.
  endmethod.
  method list_output.
    sy-lsind 0.
    set titlebar 'TIT'.
    loop at list into line.
      if line-flag 'C'.
        write / icon_ws_ship as icon.
      elseif line-flag 'T'.
        write / icon_ws_truck as icon .
      endif.
      write'Speed = 'line-speed.
    endloop.
  endmethod.
endclass.


************************************************************************
* Program events
************************************************************************

start-of-selection.
  create object list.
  set handlerlist->fcode_handler,
               list->list_change for all instances.

at user-command.
  status=>user_action).
11-20 14:36
查看更多