Sie sind auf Seite 1von 7

Ejemplo 1 (Up-Downcasting)

--------------------------
REPORT Z00_CASTING_MITARBEITER.
CLASS lcl_mita DEFINITION.
PUBLIC SECTION.
data: Vorname type c LENGTH 30,
Nachname type c LENGTH 30,
Telefon type c LENGTH 30.
ENDCLASS.
CLASS lcl_fest DEFINITION INHERITING FROM lcl_mita.
PUBLIC SECTION.
data gehalt type DECFLOAT16.
ENDCLASS.
CLASS lcl_frei DEFINITION INHERITING FROM lcl_mita.
PUBLIC SECTION.
data stundensatz type DECFLOAT16.
ENDCLASS.
INITIALIZATION.
data r_fest type REF TO lcl_fest.
data r_mita type REF TO lcl_mita.
START-OF-SELECTION.
create OBJECT r_fest.
r_fest->Vorname = 'Stefanie'.
r_fest->nachname = 'Langner'.
r_fest->gehalt = 3000.
r_mita = r_fest. " Upcasting Sicht wird auf weniger Komponenten des Objektes ein
geschrnkt
clear r_fest.
write: r_mita->Vorname,
r_mita->nachname.
r_fest ?= r_mita. "Downcasting - Sicht auf mehr Komponenten des Objektes
if r_fest is not INITIAL.
write r_fest->Gehalt.
ENDIF.
BREAK-POINT.
Ejemplo 2
---------
REPORT Z00_CASTING_MITARBEITER.
CLASS lcl_mita DEFINITION.
PUBLIC SECTION.
data: Vorname type c LENGTH 30,
Nachname type c LENGTH 30,
Telefon type c LENGTH 30.
ENDCLASS.
CLASS lcl_fest DEFINITION INHERITING FROM lcl_mita.
PUBLIC SECTION.
data gehalt type DECFLOAT16.
ENDCLASS.
CLASS lcl_frei DEFINITION INHERITING FROM lcl_mita.
PUBLIC SECTION.
data stundensatz type DECFLOAT16.
ENDCLASS.
INITIALIZATION.
data r_fest type REF TO lcl_fest.
data r_mita type REF TO lcl_mita.
data it_r_mita type TABLE OF REF TO lcl_mita.
START-OF-SELECTION.
create OBJECT r_fest.
r_fest->Vorname = 'Stefanie'.
r_fest->nachname = 'Langner'.
r_fest->gehalt = 3000.
r_mita = r_fest. " Upcasting Sicht wird auf weniger Komponenten des Objektes ein
geschrnkt
append r_mita to it_r_mita.
append r_fest to it_r_mita. " Implizites Upcasting
loop at it_r_mita into r_mita.
write: r_mita->vorname, r_mita->nachname.
r_fest ?= r_mita. "downcasting
write r_fest->gehalt.
ENDLOOP.
*clear r_fest.
*
*write: r_mita->Vorname,
* r_mita->nachname.
*
*r_fest ?= r_mita. "Downcasting - Sicht auf mehr Komponenten des Objektes
*if r_fest is not INITIAL.
*write r_fest->Gehalt.
*ENDIF.
BREAK-POINT.
Ejemplo 3
---------
REPORT z00_casting_mitarbeiter.
*----------------------------------------------------------------------*
* CLASS lcl_mita DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_mita DEFINITION.
PUBLIC SECTION.
DATA: vorname TYPE c LENGTH 30,
nachname TYPE c LENGTH 30,
telefon TYPE c LENGTH 30.
ENDCLASS. "lcl_mita DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_fest DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_fest DEFINITION INHERITING FROM lcl_mita.
PUBLIC SECTION.
DATA gehalt TYPE decfloat16.
ENDCLASS. "lcl_fest DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_frei DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_frei DEFINITION INHERITING FROM lcl_mita.
PUBLIC SECTION.
DATA stundensatz TYPE decfloat16.
ENDCLASS. "lcl_frei DEFINITION
INITIALIZATION.
DATA r_fest TYPE REF TO lcl_fest.
DATA r_frei TYPE REF TO lcl_frei.
DATA r_mita TYPE REF TO lcl_mita.
DATA it_r_mita TYPE TABLE OF REF TO lcl_mita.
START-OF-SELECTION.
CREATE OBJECT r_fest.
r_fest->vorname = 'Stefanie'.
r_fest->nachname = 'Langner'.
r_fest->gehalt = 3000.
r_mita = r_fest. " Upcasting Sicht wird auf weniger Komponenten des Objektes e
ingeschrnkt
APPEND r_mita TO it_r_mita.
APPEND r_fest TO it_r_mita. " Implizites Upcasting
LOOP AT it_r_mita INTO r_mita.
WRITE: r_mita->vorname, r_mita->nachname.
TRY.
r_fest ?= r_mita. "downcasting
WRITE r_fest->gehalt.
CATCH cx_root.
ENDTRY.
TRY.
r_frei ?= r_mita. "downcasting
WRITE r_frei->stundensatz.
CATCH cx_root.
ENDTRY.
ENDLOOP.
*clear r_fest.
*
*write: r_mita->Vorname,
* r_mita->nachname.
*
*r_fest ?= r_mita. "Downcasting - Sicht auf mehr Komponenten des Objektes
*if r_fest is not INITIAL.
*write r_fest->Gehalt.
*ENDIF.
BREAK-POINT.
Ejemplo 4
---------
class ZCL_00_MITA definition
public
create public .
public section.
*"* public components of class ZCL_00_MITA
*"* do not include other source files here!!!
data VORNAME type TEXT30 .
methods CONSTRUCTOR
importing
!IM_VORNAME type TEXT30
!IM_NACHNAME type TEXT30
!IM_TELEFON type TEXT30 .
methods DISPLAY_MITA_DATEN .
protected section.
*"* protected components of class ZCL_00_MITA
*"* do not include other source files here!!!
data NACHNAME type TEXT30 .
data TELEFON type TEXT30 .

class-data R_MITA type ref to ZCL_00_MITA .
private section.
*"* private components of class ZCL_00_MITA
*"* do not include other source files here!!!
ENDCLASS.
CLASS ZCL_00_MITA IMPLEMENTATION.
* <SIGNATURE>-------------------------------------------------------------------
--------------------+
* | Instance Public Method ZCL_00_MITA->CONSTRUCTOR
* +-----------------------------------------------------------------------------
--------------------+
* | [--->] IM_VORNAME TYPE TEXT30
* | [--->] IM_NACHNAME TYPE TEXT30
* | [--->] IM_TELEFON TYPE TEXT30
* +-----------------------------------------------------------------------------
---------</SIGNATURE>
method CONSTRUCTOR.
move: im_vorname to vorname,
im_nachname to nachname,
im_telefon to telefon.
endmethod.
* <SIGNATURE>-------------------------------------------------------------------
--------------------+
* | Instance Public Method ZCL_00_MITA->DISPLAY_MITA_DATEN
* +-----------------------------------------------------------------------------
--------------------+
* +-----------------------------------------------------------------------------
---------</SIGNATURE>
method DISPLAY_MITA_DATEN.
write: / vorname,
nachname,
Telefon.
endmethod.
ENDCLASS.
REPORT Z00_BEARBEITUNG_MITARBEITER.
CLASS lcl_fest DEFINITION INHERITING FROM zcl_00_mita.
PUBLIC SECTION.
data gehalt type i.
ENDCLASS.
data r_fest TYPE REF TO lcl_fest.
data r_mita type REF TO ZCL_00_MITA.
data it_r_mita like TABLE OF r_mita.
START-OF-SELECTION.
CREATE OBJECT r_fest
EXPORTING
im_vorname = 'Stefanie'
im_nachname = 'Langner'
im_telefon = '0228 888 777655'
.
r_fest->gehalt = 3000.
append r_fest to it_r_mita. "Impizites Upcasting
CREATE OBJECT r_fest
EXPORTING
im_vorname = 'Horst'
im_nachname = 'Langghoff'
im_telefon = '066644 777655' .
r_fest->gehalt = 4000.
append r_fest to it_r_mita. "Horst
loop at it_r_mita into r_mita.
r_mita->display_mita_daten( ).
r_fest ?= r_mita.
write r_fest->gehalt.
ENDLOOP
Ejemplo 5
---------
class ZCL_00_FEST definition
public
inheriting from ZCL_00_MITA
final
create public .
public section.
*"* public components of class ZCL_00_FEST
*"* do not include other source files here!!!
data GEHALT type INT4 .
methods DISPLAY_MITA_DATEN
redefinition .
protected section.
*"* protected components of class ZCL_00_FEST
*"* do not include other source files here!!!
private section.
*"* private components of class ZCL_00_FEST
*"* do not include other source files here!!!
ENDCLASS.
CLASS ZCL_00_FEST IMPLEMENTATION.
* <SIGNATURE>-------------------------------------------------------------------
--------------------+
* | Instance Public Method ZCL_00_FEST->DISPLAY_MITA_DATEN
* +-----------------------------------------------------------------------------
--------------------+
* +-----------------------------------------------------------------------------
---------</SIGNATURE>
method DISPLAY_MITA_DATEN.
CALL METHOD SUPER->DISPLAY_MITA_DATEN.
write: 'Gehalt', gehalt.
endmethod.
ENDCLASS.
Ejemplo 6 (aqu hemos quitado la parte r_fest?= r_mita)
---------
REPORT Z00_BEARBEITUNG_MITARBEITER.
CLASS lcl_fest DEFINITION INHERITING FROM zcl_00_mita.
PUBLIC SECTION.
METHODs display_mita_daten REDEFINITION.
data gehalt type i.
ENDCLASS.
CLASS lcl_fest IMPLEMENTATION.
METHOD display_mita_daten.
super->display_mita_daten( ).
write: 'lokales Gehalt', gehalt.
ENDMETHOD.
ENDCLASS.
data r_fest TYPE REF TO lcl_fest.
data r_mita type REF TO ZCL_00_MITA.
data it_r_mita like TABLE OF r_mita.
START-OF-SELECTION.
CREATE OBJECT r_fest
EXPORTING
im_vorname = 'Stefanie'
im_nachname = 'Langner'
im_telefon = '0228 888 777655'
.
r_fest->gehalt = 3000.
append r_fest to it_r_mita. "Impizites Upcasting
CREATE OBJECT r_fest
EXPORTING
im_vorname = 'Horst'
im_nachname = 'Langghoff'
im_telefon = '066644 777655' .
r_fest->gehalt = 4000.
append r_fest to it_r_mita. "Horst
loop at it_r_mita into r_mita.
r_mita->display_mita_daten( ).
* r_fest ?= r_mita.
* write r_fest->gehalt.
ENDLOOP.