Beispiel zur objektorientierten Programmierung

*&---------------------------------------------------------------------*
*& Report ZZ_##_OBJECTS
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT zz_##_objects.

*----------------------------------------------------------------------*
* CLASS counter DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS counter DEFINITION.
PUBLIC SECTION.
METHODS: constructor IMPORTING init_value TYPE i,
increment,
write,
whoami.
PROTECTED SECTION.
DATA value TYPE i.
ENDCLASS. "counter DEFINITION

*----------------------------------------------------------------------*
* CLASS counter IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS counter IMPLEMENTATION.
METHOD constructor.
value = init_value.
ENDMETHOD. "constructor
METHOD increment.
ADD 1 TO value.
ENDMETHOD. "increment
METHOD write.
WRITE / value.
ENDMETHOD. "write
METHOD whoami.
WRITE /'Ich bin der Basiszähler'.
ENDMETHOD. "whoami
ENDCLASS. "counter IMPLEMENTATION

*----------------------------------------------------------------------*
* CLASS counter_10 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS counter_10 DEFINITION INHERITING FROM counter.
PUBLIC SECTION.
METHODS: constructor IMPORTING init_value TYPE i,
increment REDEFINITION,
write REDEFINITION,
whoami REDEFINITION.
DATA value_10 TYPE i.
ENDCLASS. "counter_10 DEFINITION

*----------------------------------------------------------------------*
* CLASS counter_10 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS counter_10 IMPLEMENTATION.
METHOD constructor.
CALL METHOD super->constructor
EXPORTING
init_value = init_value.
value_10 = value DIV 10.
ENDMETHOD. "constructor
METHOD increment.
CALL METHOD super->increment.
value_10 = value DIV 10.
ENDMETHOD. "increment
METHOD write.
CALL METHOD super->write.
WRITE value_10.
ENDMETHOD. "write
METHOD whoami.
WRITE /'Ich bin der Zehnerzähler'.
ENDMETHOD. "whoami
ENDCLASS. "counter_10 IMPLEMENTATION

*----------------------------------------------------------------------*
* CLASS counter_20 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS counter_20 DEFINITION INHERITING FROM counter.
PUBLIC SECTION.
METHODS: constructor IMPORTING init_value TYPE i,
increment REDEFINITION,
write REDEFINITION,
whoami REDEFINITION.
DATA value_20 TYPE i.
ENDCLASS. "counter_20 DEFINITION

*----------------------------------------------------------------------*
* CLASS counter_20 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS counter_20 IMPLEMENTATION.
METHOD constructor.
CALL METHOD super->constructor
EXPORTING
init_value = init_value.
value_20 = value DIV 20.
ENDMETHOD. "constructor
METHOD increment.
CALL METHOD super->increment.
value_20 = value DIV 20.
ENDMETHOD. "increment
METHOD write.
CALL METHOD super->write.
WRITE value_20.
ENDMETHOD. "write
METHOD whoami.
WRITE /'Ich bin der Zwanzigerzähler'.
ENDMETHOD. "whoami
ENDCLASS. "counter_20 IMPLEMENTATION

DATA: count TYPE REF TO counter,
number TYPE i VALUE 0.

DATA: a TYPE REF TO counter,
b TYPE REF TO counter,
c TYPE REF TO counter,
x TYPE REF TO counter,
y TYPE REF TO counter,
z TYPE REF TO counter.

START-OF-SELECTION.

CREATE OBJECT count
EXPORTING
init_value = number.

DO 42 TIMES.
CALL METHOD count->increment.
CALL METHOD count->write.
ENDDO.

ULINE.

CREATE OBJECT count
TYPE
counter_10
EXPORTING
init_value = number.

DO 42 TIMES.
CALL METHOD count->increment.
CALL METHOD count->write.
ENDDO.

ULINE.

CREATE OBJECT count
TYPE
counter_20
EXPORTING
init_value = number.

DO 42 TIMES.
CALL METHOD count->increment.
CALL METHOD count->write.
ENDDO.

ULINE.

CREATE OBJECT a
EXPORTING
init_value = number.

CREATE OBJECT b
TYPE
counter_10
EXPORTING
init_value = number.

CREATE OBJECT c
TYPE
counter_20
EXPORTING
init_value = number.

CREATE OBJECT x
EXPORTING
init_value = number.

CREATE OBJECT y
TYPE
counter_10
EXPORTING
init_value = number.

CREATE OBJECT z
TYPE
counter_20
EXPORTING
init_value = number.

CALL METHOD a->whoami.
CALL METHOD b->whoami.
CALL METHOD c->whoami.
CALL METHOD x->whoami.
CALL METHOD y->whoami.
CALL METHOD z->whoami.