ABAP Objects Basics Tutorial

Aus MattWiki
Version vom 11. April 2018, 10:18 Uhr von Matt (Diskussion | Beiträge) (Die Seite wurde neu angelegt: „Nachfolgender Artikel enhält die Programmcodes, die aus dem ABAP Objects Basic Guided Tutorial von SAP (https://archive.sap.com/documents/docs/DOC-10394) ents…“)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)

Nachfolgender Artikel enhält die Programmcodes, die aus dem ABAP Objects Basic Guided Tutorial von SAP (https://archive.sap.com/documents/docs/DOC-10394) entstanden sind.

UML Klassendiagramm

Die in dem Tutorial entwickelten Klassen stellen sich in einem UML Klassendiagramm wie folgt dar:


Zu entwickelnde Klassen

ZCL_VEHICLE_MO

*----------------------------------------------------------------------*
*       CLASS ZCL_VEHICLE_MO DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class ZCL_VEHICLE_MO definition
  public
  create public .

public section.

  interfaces ZIF_STATUS_MO .

  aliases SHOW
    for ZIF_STATUS_MO~SHOW .

  methods CONSTRUCTOR .
  methods SPEED_UP
    importing
      !STEP type I
    raising
      ZCX_OVERHEATING_MO .
  methods STOP .
  PROTECTED SECTION.

    DATA speed TYPE i .
    DATA max_speed TYPE i VALUE 50.   "#EC NOTEXT .  .  .           " .
  PRIVATE SECTION.

    DATA id TYPE i .
    CLASS-DATA object_count TYPE i .
ENDCLASS.



CLASS ZCL_VEHICLE_MO IMPLEMENTATION.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_VEHICLE_MO->CONSTRUCTOR
* +-------------------------------------------------------------------------------------------------+
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD constructor.
    object_count = object_count + 1.
    id = object_count.
  ENDMETHOD.                    "CONSTRUCTOR


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_VEHICLE_MO->SPEED_UP
* +-------------------------------------------------------------------------------------------------+
* | [--->] STEP                           TYPE        I
* | [!CX!] ZCX_OVERHEATING_MO
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD speed_up.
    speed = speed + step.

    IF speed > max_speed.
      RAISE EXCEPTION TYPE zcx_overheating_mo.
    ENDIF.

  ENDMETHOD.                    "SPEED_UP


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_VEHICLE_MO->STOP
* +-------------------------------------------------------------------------------------------------+
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD stop.
    speed = 0.
  ENDMETHOD.                    "stop


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_VEHICLE_MO->ZIF_STATUS_MO~SHOW
* +-------------------------------------------------------------------------------------------------+
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD zif_status_mo~show.
    DATA msg TYPE string.
    msg = `Vehicle ` && |{ id }| &&
          `, Speed = ` && |{ speed }| &&
          `, Max-Speed = ` && |{ max_speed }| .
    MESSAGE msg TYPE 'I'.
  ENDMETHOD.
ENDCLASS.

ZCL_APPLICATION_MO

Diese Klasse enhält die Methode START und führt die übrigen Klassen aus.

METHOD start.
  DATA: lr_status TYPE REF TO zif_status_mo,
        lt_status LIKE TABLE OF lr_status,
        lr_truck TYPE REF TO zcl_truck_mo,
        lr_ship TYPE REF TO zcl_ship_mo,
        lr_heli TYPE REF TO zcl_helicopter_mo.

  CREATE OBJECT:
  lr_truck,
  lr_ship EXPORTING name = 'Titanic',
  lr_heli.

  APPEND:
  lr_truck TO lt_status,
  lr_ship TO lt_status,
  lr_heli TO lt_status.

  SET HANDLER lr_heli->receive FOR ALL INSTANCES.

  TRY.
      lr_truck->speed_up( 30 ).
      lr_ship->speed_up( 10 ).

      LOOP AT lt_status INTO lr_status.
        lr_status->show( ).
      ENDLOOP.

      DO 5 TIMES.
        lr_ship->speed_up( 10 ).
      ENDDO.

      DO 5 TIMES.
        lr_truck->speed_up( 30 ).
        lr_truck->show( ).
      ENDDO.
    CATCH zcx_overheating_mo.
      MESSAGE 'Truck overheated' TYPE 'I'.
      EXIT.
  ENDTRY.
ENDMETHOD.