ABAP Objects Basics Tutorial: Unterschied zwischen den Versionen
Aus MattWiki
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…“) |
Matt (Diskussion | Beiträge) Keine Bearbeitungszusammenfassung |
||
Zeile 99: | Zeile 99: | ||
MESSAGE msg TYPE 'I'. | MESSAGE msg TYPE 'I'. | ||
ENDMETHOD. | ENDMETHOD. | ||
ENDCLASS. | |||
</syntaxhighlight> | |||
=== ZCL_TRUCK_MO - erbt von ZCL_VEHICLE_MO === | |||
<syntaxhighlight lang="abap"> | |||
class ZCL_TRUCK_MO definition | |||
public | |||
inheriting from ZCL_VEHICLE_MO | |||
final | |||
create public . | |||
public section. | |||
methods CONSTRUCTOR . | |||
methods SHOW | |||
redefinition . | |||
protected section. | |||
private section. | |||
ENDCLASS. | |||
CLASS ZCL_TRUCK_MO IMPLEMENTATION. | |||
* <SIGNATURE>---------------------------------------------------------------------------------------+ | |||
* | Instance Public Method ZCL_TRUCK_MO->CONSTRUCTOR | |||
* +-------------------------------------------------------------------------------------------------+ | |||
* +--------------------------------------------------------------------------------------</SIGNATURE> | |||
METHOD constructor. | |||
super->constructor( ). | |||
max_speed = 100. | |||
ENDMETHOD. | |||
* <SIGNATURE>---------------------------------------------------------------------------------------+ | |||
* | Instance Private Method ZCL_TRUCK_MO->SHOW | |||
* +-------------------------------------------------------------------------------------------------+ | |||
* +--------------------------------------------------------------------------------------</SIGNATURE> | |||
METHOD show. | |||
DATA msg TYPE string. | |||
msg = `Truck ` && | |||
`, Speed = ` && |{ speed }| && | |||
`, Max Speed = ` && |{ max_speed }|. | |||
MESSAGE msg TYPE 'I'. | |||
ENDMETHOD. "show | |||
ENDCLASS. | |||
</syntaxhighlight> | |||
=== ZCL_SHIP_MO - erbt von ZCL_VEHICLE_MO, definiert Ereignis === | |||
<syntaxhighlight lang="abap"> | |||
class ZCL_SHIP_MO definition | |||
public | |||
inheriting from ZCL_VEHICLE_MO | |||
final | |||
create public . | |||
public section. | |||
data NAME type STRING . | |||
events DAMAGED . | |||
methods CONSTRUCTOR | |||
importing | |||
!NAME type STRING . | |||
methods SHOW | |||
redefinition . | |||
methods SPEED_UP | |||
redefinition . | |||
protected section. | |||
private section. | |||
ENDCLASS. | |||
CLASS ZCL_SHIP_MO IMPLEMENTATION. | |||
* <SIGNATURE>---------------------------------------------------------------------------------------+ | |||
* | Instance Public Method ZCL_SHIP_MO->CONSTRUCTOR | |||
* +-------------------------------------------------------------------------------------------------+ | |||
* | [--->] NAME TYPE STRING | |||
* +--------------------------------------------------------------------------------------</SIGNATURE> | |||
METHOD constructor. | |||
super->constructor( ). | |||
max_speed = 30. | |||
me->name = name. | |||
ENDMETHOD. | |||
* <SIGNATURE>---------------------------------------------------------------------------------------+ | |||
* | Instance Private Method ZCL_SHIP_MO->SHOW | |||
* +-------------------------------------------------------------------------------------------------+ | |||
* +--------------------------------------------------------------------------------------</SIGNATURE> | |||
METHOD show. | |||
*CALL METHOD SUPER->SHOW | |||
* . | |||
DATA msg TYPE string. | |||
msg = me->name && | |||
`, Speed = ` && |{ speed }| && | |||
`, Max Speed = ` && |{ max_speed }|. | |||
MESSAGE msg TYPE 'I'. | |||
ENDMETHOD. | |||
* <SIGNATURE>---------------------------------------------------------------------------------------+ | |||
* | Instance Public Method ZCL_SHIP_MO->SPEED_UP | |||
* +-------------------------------------------------------------------------------------------------+ | |||
* | [--->] STEP TYPE I | |||
* | [!CX!] ZCX_OVERHEATING_MO | |||
* +--------------------------------------------------------------------------------------</SIGNATURE> | |||
METHOD speed_up. | |||
*CALL METHOD SUPER->SPEED_UP | |||
* EXPORTING | |||
* STEP = | |||
* . | |||
speed = speed + step. | |||
IF speed > max_speed. | |||
max_speed = 0. | |||
CALL METHOD stop. | |||
RAISE EVENT damaged. | |||
ENDIF. | |||
ENDMETHOD. | |||
ENDCLASS. | ENDCLASS. | ||
</syntaxhighlight> | </syntaxhighlight> |
Version vom 11. April 2018, 10:22 Uhr
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_TRUCK_MO - erbt von ZCL_VEHICLE_MO
class ZCL_TRUCK_MO definition
public
inheriting from ZCL_VEHICLE_MO
final
create public .
public section.
methods CONSTRUCTOR .
methods SHOW
redefinition .
protected section.
private section.
ENDCLASS.
CLASS ZCL_TRUCK_MO IMPLEMENTATION.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_TRUCK_MO->CONSTRUCTOR
* +-------------------------------------------------------------------------------------------------+
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD constructor.
super->constructor( ).
max_speed = 100.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Private Method ZCL_TRUCK_MO->SHOW
* +-------------------------------------------------------------------------------------------------+
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD show.
DATA msg TYPE string.
msg = `Truck ` &&
`, Speed = ` && |{ speed }| &&
`, Max Speed = ` && |{ max_speed }|.
MESSAGE msg TYPE 'I'.
ENDMETHOD. "show
ENDCLASS.
ZCL_SHIP_MO - erbt von ZCL_VEHICLE_MO, definiert Ereignis
class ZCL_SHIP_MO definition
public
inheriting from ZCL_VEHICLE_MO
final
create public .
public section.
data NAME type STRING .
events DAMAGED .
methods CONSTRUCTOR
importing
!NAME type STRING .
methods SHOW
redefinition .
methods SPEED_UP
redefinition .
protected section.
private section.
ENDCLASS.
CLASS ZCL_SHIP_MO IMPLEMENTATION.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_SHIP_MO->CONSTRUCTOR
* +-------------------------------------------------------------------------------------------------+
* | [--->] NAME TYPE STRING
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD constructor.
super->constructor( ).
max_speed = 30.
me->name = name.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Private Method ZCL_SHIP_MO->SHOW
* +-------------------------------------------------------------------------------------------------+
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD show.
*CALL METHOD SUPER->SHOW
* .
DATA msg TYPE string.
msg = me->name &&
`, Speed = ` && |{ speed }| &&
`, Max Speed = ` && |{ max_speed }|.
MESSAGE msg TYPE 'I'.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_SHIP_MO->SPEED_UP
* +-------------------------------------------------------------------------------------------------+
* | [--->] STEP TYPE I
* | [!CX!] ZCX_OVERHEATING_MO
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD speed_up.
*CALL METHOD SUPER->SPEED_UP
* EXPORTING
* STEP =
* .
speed = speed + step.
IF speed > max_speed.
max_speed = 0.
CALL METHOD stop.
RAISE EVENT damaged.
ENDIF.
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.