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 |
||
(3 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt) | |||
Zeile 5: | Zeile 5: | ||
Die in dem Tutorial entwickelten Klassen stellen sich in einem UML Klassendiagramm wie folgt dar: | Die in dem Tutorial entwickelten Klassen stellen sich in einem UML Klassendiagramm wie folgt dar: | ||
[[Datei:ABAP Objects Basic Guided Tutorial UML Class Diagramm.png]] | |||
== | == Klassen == | ||
=== ZCL_VEHICLE_MO === | === ZCL_VEHICLE_MO === | ||
Zeile 102: | Zeile 100: | ||
</syntaxhighlight> | </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 - 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. | |||
</syntaxhighlight> | |||
=== ZCL_HELICOPTER_MO - Empfängt Ereignis === | |||
<syntaxhighlight lang="abap"> | |||
class ZCL_HELICOPTER_MO definition | |||
public | |||
final | |||
create public . | |||
public section. | |||
interfaces ZIF_STATUS_MO . | |||
aliases SHOW | |||
for ZIF_STATUS_MO~SHOW . | |||
methods RECEIVE | |||
for event DAMAGED of ZCL_SHIP_MO | |||
importing | |||
!SENDER . | |||
protected section. | |||
private section. | |||
ENDCLASS. | |||
CLASS ZCL_HELICOPTER_MO IMPLEMENTATION. | |||
* <SIGNATURE>---------------------------------------------------------------------------------------+ | |||
* | Instance Public Method ZCL_HELICOPTER_MO->RECEIVE | |||
* +-------------------------------------------------------------------------------------------------+ | |||
* | [--->] SENDER LIKE | |||
* +--------------------------------------------------------------------------------------</SIGNATURE> | |||
METHOD receive. | |||
DATA msg TYPE string. | |||
msg = `Helicopter received call from ` && sender->name. | |||
MESSAGE msg TYPE 'I'. | |||
ENDMETHOD. | |||
* <SIGNATURE>---------------------------------------------------------------------------------------+ | |||
* | Instance Public Method ZCL_HELICOPTER_MO->ZIF_STATUS_MO~SHOW | |||
* +-------------------------------------------------------------------------------------------------+ | |||
* +--------------------------------------------------------------------------------------</SIGNATURE> | |||
METHOD zif_status_mo~show. | |||
DATA msg TYPE string. | |||
msg = 'Helicopter, idle'. | |||
MESSAGE msg TYPE 'I'. | |||
ENDMETHOD. | |||
ENDCLASS. | |||
</syntaxhighlight> | |||
=== ZCX_OVERHEATING_MO === | |||
Ausnahmeklasse für TRY-CATCH-Kontrollstruktur. | |||
=== ZCL_APPLICATION_MO - Startet und nutzt Klassen === | |||
<syntaxhighlight lang="abap"> | <syntaxhighlight lang="abap"> | ||
Zeile 147: | Zeile 332: | ||
ENDTRY. | ENDTRY. | ||
ENDMETHOD. | ENDMETHOD. | ||
</syntaxhighlight> | |||
== Interface ZIF_STATUS_MO == | |||
<syntaxhighlight lang="abap"> | |||
interface ZIF_STATUS_MO | |||
public . | |||
methods SHOW . | |||
endinterface. | |||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:ABAP]] | [[Category:ABAP]] |
Aktuelle Version vom 11. April 2018, 10:39 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:
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 - 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_HELICOPTER_MO - Empfängt Ereignis
class ZCL_HELICOPTER_MO definition
public
final
create public .
public section.
interfaces ZIF_STATUS_MO .
aliases SHOW
for ZIF_STATUS_MO~SHOW .
methods RECEIVE
for event DAMAGED of ZCL_SHIP_MO
importing
!SENDER .
protected section.
private section.
ENDCLASS.
CLASS ZCL_HELICOPTER_MO IMPLEMENTATION.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_HELICOPTER_MO->RECEIVE
* +-------------------------------------------------------------------------------------------------+
* | [--->] SENDER LIKE
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD receive.
DATA msg TYPE string.
msg = `Helicopter received call from ` && sender->name.
MESSAGE msg TYPE 'I'.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_HELICOPTER_MO->ZIF_STATUS_MO~SHOW
* +-------------------------------------------------------------------------------------------------+
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD zif_status_mo~show.
DATA msg TYPE string.
msg = 'Helicopter, idle'.
MESSAGE msg TYPE 'I'.
ENDMETHOD.
ENDCLASS.
ZCX_OVERHEATING_MO
Ausnahmeklasse für TRY-CATCH-Kontrollstruktur.
ZCL_APPLICATION_MO - Startet und nutzt Klassen
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.
Interface ZIF_STATUS_MO
interface ZIF_STATUS_MO
public .
methods SHOW .
endinterface.