Übung Schrödinger: 09 Vererbung (ABAP)
Aus MattWiki
Beispiellisting für Vererbung in lokalen Klassen aus Kapitel 09 im Buch Schrödinger programmiert ABAP.
<syntaxhighight lang="abap"> REPORT zsch_matt_beer.
CLASS lcl_bier DEFINITION.
PUBLIC SECTION. DATA: gd_stammwuerze TYPE p LENGTH 3 DECIMALS 2 VALUE '12.50'. TYPES: gdt_alkoholgehalt LIKE gd_stammwuerze. METHODS: write_stammwuerze, write_volumen, set_alkoholgehalt IMPORTING id_alkoholgehalt TYPE gdt_alkoholgehalt, brauen. PROTECTED SECTION. DATA: gd_volumen TYPE p LENGTH 4 DECIMALS 2 VALUE 333. PRIVATE SECTION. DATA: gd_alkoholgehalt TYPE gdt_alkoholgehalt VALUE '4.50'.
ENDCLASS.
CLASS lcl_bier IMPLEMENTATION.
METHOD write_stammwuerze. WRITE: / `Stammwürze: `, me->gd_stammwuerze. ENDMETHOD.
METHOD write_volumen. WRITE: / `Volumen: `, me->gd_volumen. ENDMETHOD.
METHOD set_alkoholgehalt. me->gd_alkoholgehalt = id_alkoholgehalt. ENDMETHOD.
METHOD brauen. WRITE: / 'Ich braue und braue Bier den ganzen lieben langen Tag.'. ENDMETHOD.
ENDCLASS.
CLASS lcl_bier_untergaerig DEFINITION INHERITING FROM lcl_bier.
PUBLIC SECTION. METHODS: brauen REDEFINITION. PROTECTED SECTION. PRIVATE SECTION.
ENDCLASS.
CLASS lcl_bier_untergaerig IMPLEMENTATION.
METHOD brauen. CALL METHOD super->brauen. WRITE: / 'Sogar untergärig'. ENDMETHOD.
ENDCLASS.
CLASS lcl_koelsch DEFINITION INHERITING FROM lcl_bier.
PUBLIC SECTION. PROTECTED SECTION. PRIVATE SECTION.
ENDCLASS.
CLASS lcl_koelsch IMPLEMENTATION. ENDCLASS.
DATA: gr_untergaerig TYPE REF TO lcl_bier_untergaerig.
START-OF-SELECTION.
CREATE OBJECT gr_untergaerig. CALL METHOD gr_untergaerig->write_stammwuerze. CALL METHOD gr_untergaerig->write_volumen. CALL METHOD gr_untergaerig->brauen.
</syntaxhighlight>