SAP ABAP CODES

Page 1

1|Page *&---------------------------------------------------------------------* *& Report Z00_OBJ01_PROC_PROGRAMMING *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

Z00_OBJ01_PROC_PROGRAMMING.

DATA: GT_SFLIGHT TYPE TABLE OF SFLIGHT, GS_SFLIGHT TYPE SFLIGHT. START-OF-SELECTION. SELECT * FROM SFLIGHT INTO TABLE GT_SFLIGHT. LOOP AT GT_SFLIGHT INTO GS_SFLIGHT. ADD 10 TO GS_SFLIGHT-SEATSOCC. IF GS_SFLIGHT-SEATSOCC GT GS_SFLIGHT-SEATSMAX. GS_SFLIGHT-SEATSOCC = GS_SFLIGHT-SEATSMAX. ENDIF. WRITE: / GS_SFLIGHT-CARRID, GS_SFLIGHT-CONNID, GS_SFLIGHT-FLDATE, GS_SFLIGHT-PLANETYPE, GS_SFLIGHT-SEATSMAX, GS_SFLIGHT-SEATSOCC. ENDLOOP.

*&---------------------------------------------------------------------* *& Report Z00_OBJ02_USE_VEHICLEFG *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

Z00_OBJ02_USE_VEHICLEFG.

START-OF-SELECTION. CALL FUNCTION 'Z_VEHICLE_SET_ATTRIBUTES00' EXPORTING I_REGNO = 'AAA1111' I_COLOR = 'RED' I_SPEED = 50 . CALL FUNCTION 'Z_VEHICLE_ACCELERATE00' EXPORTING I_DSPEED = 20 . CALL FUNCTION 'Z_VEHICLE_DECELERATE00' EXPORTING


2|Page I_DSPEED .

= 15

CALL FUNCTION 'Z_VEHICLE_DISPLAY_ATTRIBUTES00' . *&---------------------------------------------------------------------* *& Report Z00_OBJ03_VEHICLE1 *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

Z00_OBJ03_VEHICLE1.

CLASS LCL_VEHICLE DEFINITION. PUBLIC SECTION. DATA: REGNO TYPE ZREGNO READ-ONLY. METHODS: SET_ATTRIBUTES IMPORTING I_REGNO TYPE ZREGNO I_COLOR TYPE ZCOLOR I_SPEED TYPE ZSPEED, DISPLAY_ATTRIBUTES, ACCELERATE IMPORTING I_DSPEED TYPE ZSPEED, DECELERATE IMPORTING I_DSPEED TYPE ZSPEED, GET_COLOR RETURNING VALUE(R_COLOR) TYPE ZCOLOR, SET_COLOR IMPORTING I_COLOR TYPE ZCOLOR. PRIVATE SECTION. DATA: COLOR TYPE ZCOLOR, SPEED TYPE ZSPEED. ENDCLASS. CLASS LCL_VEHICLE IMPLEMENTATION. METHOD SET_ATTRIBUTES. REGNO = I_REGNO. COLOR = I_COLOR. SPEED = I_SPEED. ENDMETHOD. METHOD DISPLAY_ATTRIBUTES. WRITE: / 'Attributes of vehicle:', 'REGNO =', REGNO, 'COLOR =', COLOR, 'SPEED =', SPEED. ENDMETHOD. METHOD ACCELERATE. ADD I_DSPEED TO SPEED. ENDMETHOD. METHOD DECELERATE.


3|Page IF I_DSPEED GT SPEED. WRITE: / 'Unable to decelerate below zero'. EXIT. ENDIF. SUBTRACT I_DSPEED FROM SPEED. ENDMETHOD.

" SPEED = SPEED - I_DSPEED.

METHOD GET_COLOR. R_COLOR = COLOR. ENDMETHOD. METHOD SET_COLOR. COLOR = I_COLOR. ENDMETHOD. ENDCLASS.

DATA: GO_VEHICLE1 GO_VEHICLE2 GO_VEHICLE3

TYPE REF TO LCL_VEHICLE, LIKE GO_VEHICLE1, TYPE REF TO LCL_VEHICLE.

START-OF-SELECTION. CREATE OBJECT GO_VEHICLE1. CREATE OBJECT GO_VEHICLE2. * GO_VEHICLE3 points to the same object as GO_VEHICLE1. GO_VEHICLE3 = GO_VEHICLE1.

* GO_VEHICLE2 points to the same object as GO_VEHICLE1. * GO_VEHICLE2 will dereference the previous object. GO_VEHICLE2 = GO_VEHICLE1. * Public Read-only attributes cannot be written from outside the class * GO_VEHICLE1->REGNO = 'AAA1111'. * GO_VEHICLE2->REGNO = 'BBB2222'. * Read / write access to private attributes not allowed * GO_VEHICLE1->COLOR = 'RED'. * WRITE: / 'GO_VEHICLE1->COLOR =', GO_VEHICLE1->COLOR. * Public read-only attributes can be read from outside the class WRITE: / 'GO_VEHICLE1->REGNO =', GO_VEHICLE1->REGNO. WRITE: / 'GO_VEHICLE2->REGNO =', GO_VEHICLE2->REGNO.

*&---------------------------------------------------------------------* *& Report Z00_OBJ03_VEHICLE1 *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

Z00_OBJ04_BUFFERING_INT_TABLES.


4|Page CLASS LCL_VEHICLE DEFINITION. PUBLIC SECTION. DATA: REGNO TYPE ZREGNO READ-ONLY. METHODS: SET_ATTRIBUTES IMPORTING I_REGNO TYPE ZREGNO I_COLOR TYPE ZCOLOR I_SPEED TYPE ZSPEED, DISPLAY_ATTRIBUTES, ACCELERATE IMPORTING I_DSPEED TYPE ZSPEED, DECELERATE IMPORTING I_DSPEED TYPE ZSPEED, GET_COLOR RETURNING VALUE(R_COLOR) TYPE ZCOLOR, SET_COLOR IMPORTING I_COLOR TYPE ZCOLOR. PRIVATE SECTION. DATA: COLOR TYPE ZCOLOR, SPEED TYPE ZSPEED. ENDCLASS. CLASS LCL_VEHICLE IMPLEMENTATION. METHOD SET_ATTRIBUTES. REGNO = I_REGNO. COLOR = I_COLOR. SPEED = I_SPEED. ENDMETHOD. METHOD DISPLAY_ATTRIBUTES. WRITE: / 'Attributes of vehicle:', 'REGNO =', REGNO, 'COLOR =', COLOR, 'SPEED =', SPEED. ENDMETHOD. METHOD ACCELERATE. ADD I_DSPEED TO SPEED. ENDMETHOD. METHOD DECELERATE. IF I_DSPEED GT SPEED. WRITE: / 'Unable to decelerate below zero'. EXIT. ENDIF. SUBTRACT I_DSPEED FROM SPEED. ENDMETHOD. METHOD GET_COLOR. R_COLOR = COLOR. ENDMETHOD. METHOD SET_COLOR. COLOR = I_COLOR. ENDMETHOD.

" SPEED = SPEED - I_DSPEED.


5|Page ENDCLASS.

DATA: GO_VEHICLE GT_VEHICLE

TYPE REF TO LCL_VEHICLE, TYPE TABLE OF REF TO LCL_VEHICLE.

START-OF-SELECTION. CREATE OBJECT GO_VEHICLE. APPEND GO_VEHICLE TO GT_VEHICLE. CREATE OBJECT GO_VEHICLE. APPEND GO_VEHICLE TO GT_VEHICLE. CREATE OBJECT GO_VEHICLE. APPEND GO_VEHICLE TO GT_VEHICLE. WRITE: / 'DONE'.

*&---------------------------------------------------------------------* *& Report Z00_OBJ03_VEHICLE1 *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

Z00_OBJ05_CALLING_METHODS.

CLASS LCL_VEHICLE DEFINITION. PUBLIC SECTION. DATA: REGNO TYPE ZREGNO READ-ONLY. METHODS: SET_ATTRIBUTES IMPORTING I_REGNO TYPE ZREGNO I_COLOR TYPE ZCOLOR I_SPEED TYPE ZSPEED, DISPLAY_ATTRIBUTES, ACCELERATE IMPORTING I_DSPEED TYPE ZSPEED, DECELERATE IMPORTING I_DSPEED TYPE ZSPEED, GET_COLOR RETURNING VALUE(R_COLOR) TYPE ZCOLOR, SET_COLOR IMPORTING I_COLOR TYPE ZCOLOR. PRIVATE SECTION. DATA: COLOR TYPE ZCOLOR, SPEED TYPE ZSPEED. ENDCLASS. CLASS LCL_VEHICLE IMPLEMENTATION. METHOD SET_ATTRIBUTES. REGNO = I_REGNO. COLOR = I_COLOR.


6|Page SPEED = I_SPEED. ENDMETHOD. METHOD DISPLAY_ATTRIBUTES. WRITE: / 'Attributes of vehicle:', 'REGNO =', REGNO, 'COLOR =', COLOR, 'SPEED =', SPEED. ENDMETHOD. METHOD ACCELERATE. ADD I_DSPEED TO SPEED. ENDMETHOD. METHOD DECELERATE. IF I_DSPEED GT SPEED. WRITE: / 'Unable to decelerate below zero'. EXIT. ENDIF. SUBTRACT I_DSPEED FROM SPEED. ENDMETHOD.

" SPEED = SPEED - I_DSPEED.

METHOD GET_COLOR. R_COLOR = COLOR. ENDMETHOD. METHOD SET_COLOR. COLOR = I_COLOR. ENDMETHOD. ENDCLASS.

DATA: GO_VEHICLE GT_VEHICLE

TYPE REF TO LCL_VEHICLE, TYPE TABLE OF REF TO LCL_VEHICLE.

DATA: GV_COLOR

TYPE ZCOLOR.

START-OF-SELECTION. CREATE OBJECT GO_VEHICLE. CALL METHOD GO_VEHICLE->SET_ATTRIBUTES EXPORTING I_REGNO = 'AAA1111' I_COLOR = 'RED' I_SPEED = 50. APPEND GO_VEHICLE TO GT_VEHICLE. CREATE OBJECT GO_VEHICLE. GO_VEHICLE->SET_ATTRIBUTES( EXPORTING I_REGNO = 'BBB2222' I_COLOR = 'GREEN' I_SPEED = 90 ). APPEND GO_VEHICLE TO GT_VEHICLE. CREATE OBJECT GO_VEHICLE. GO_VEHICLE->SET_ATTRIBUTES(


7|Page I_REGNO = 'CCC3333' I_COLOR = 'BLUE' I_SPEED = 20 ). APPEND GO_VEHICLE TO GT_VEHICLE.

*

LOOP AT GT_VEHICLE INTO GO_VEHICLE. CALL METHOD GO_VEHICLE->DISPLAY_ATTRIBUTES. GO_VEHICLE->DISPLAY_ATTRIBUTES( ). ENDLOOP. CALL METHOD GO_VEHICLE->GET_COLOR RECEIVING R_COLOR = GV_COLOR. GV_COLOR = GO_VEHICLE->GET_COLOR( ). IF GO_VEHICLE->GET_COLOR( ) EQ 'RED'. WRITE: / 'It is red'. ENDIF.

*

WRITE: / GO_VEHICLE->GET_COLOR( ).

*&---------------------------------------------------------------------* *& Report Z00_OBJ03_VEHICLE1 *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

Z00_OBJ06_STATIC.

CLASS LCL_VEHICLE DEFINITION. PUBLIC SECTION. DATA: REGNO TYPE ZREGNO READ-ONLY. METHODS: SET_ATTRIBUTES IMPORTING I_REGNO TYPE ZREGNO I_COLOR TYPE ZCOLOR I_SPEED TYPE ZSPEED, DISPLAY_ATTRIBUTES, ACCELERATE IMPORTING I_DSPEED TYPE ZSPEED, DECELERATE IMPORTING I_DSPEED TYPE ZSPEED, GET_COLOR RETURNING VALUE(R_COLOR) TYPE ZCOLOR, SET_COLOR IMPORTING I_COLOR TYPE ZCOLOR. CLASS-METHODS: DISPLAY_NUMVEHICLES. PRIVATE SECTION. DATA: COLOR TYPE ZCOLOR, SPEED TYPE ZSPEED. CLASS-DATA: NUMVEHICLES TYPE I.


8|Page ENDCLASS. CLASS LCL_VEHICLE IMPLEMENTATION. METHOD SET_ATTRIBUTES. REGNO = I_REGNO. COLOR = I_COLOR. SPEED = I_SPEED. ADD 1 TO NUMVEHICLES. ENDMETHOD. METHOD DISPLAY_NUMVEHICLES. WRITE: / 'Number of vehicles on the road =', NUMVEHICLES. * WRITE: / 'REGNO =', REGNO. ENDMETHOD. METHOD DISPLAY_ATTRIBUTES. WRITE: / 'Attributes of vehicle:', 'REGNO =', REGNO, 'COLOR =', COLOR, 'SPEED =', SPEED. ENDMETHOD. METHOD ACCELERATE. ADD I_DSPEED TO SPEED. ENDMETHOD. METHOD DECELERATE. IF I_DSPEED GT SPEED. WRITE: / 'Unable to decelerate below zero'. EXIT. ENDIF. SUBTRACT I_DSPEED FROM SPEED. ENDMETHOD.

" SPEED = SPEED - I_DSPEED.

METHOD GET_COLOR. R_COLOR = COLOR. ENDMETHOD. METHOD SET_COLOR. COLOR = I_COLOR. ENDMETHOD. ENDCLASS.

DATA: GO_VEHICLE1 GO_VEHICLE2 GO_VEHICLE3

TYPE REF TO LCL_VEHICLE, TYPE REF TO LCL_VEHICLE, TYPE REF TO LCL_VEHICLE.

START-OF-SELECTION. * * *

GO_VEHICLE1->DISPLAY_NUMVEHICLES( ). GO_VEHICLE2->DISPLAY_NUMVEHICLES( ). GO_VEHICLE3->DISPLAY_NUMVEHICLES( ). LCL_VEHICLE=>DISPLAY_NUMVEHICLES( ). CREATE OBJECT GO_VEHICLE1.


9|Page CALL METHOD GO_VEHICLE1->SET_ATTRIBUTES EXPORTING I_REGNO = 'AAA1111' I_COLOR = 'RED' I_SPEED = 50. CALL METHOD GO_VEHICLE1->DISPLAY_ATTRIBUTES. CREATE OBJECT GO_VEHICLE2. GO_VEHICLE2->SET_ATTRIBUTES( EXPORTING I_REGNO = 'BBB2222' I_COLOR = 'GREEN' I_SPEED = 90 ). GO_VEHICLE2->DISPLAY_ATTRIBUTES( ). CREATE OBJECT GO_VEHICLE3. GO_VEHICLE3->SET_ATTRIBUTES( I_REGNO = 'CCC3333' I_COLOR = 'BLUE' I_SPEED = 20 ). GO_VEHICLE2->DISPLAY_ATTRIBUTES( ).

GO_VEHICLE1->DISPLAY_NUMVEHICLES( GO_VEHICLE2->DISPLAY_NUMVEHICLES( GO_VEHICLE3->DISPLAY_NUMVEHICLES( LCL_VEHICLE=>DISPLAY_NUMVEHICLES(

). ). ). ).

*&---------------------------------------------------------------------* *& Report Z00_OBJ03_VEHICLE1 *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

Z00_OBJ07_CONSTRUCTOR.

CLASS LCL_VEHICLE DEFINITION. PUBLIC SECTION. DATA: REGNO TYPE ZREGNO READ-ONLY. METHODS: CONSTRUCTOR IMPORTING I_REGNO TYPE ZREGNO I_COLOR TYPE ZCOLOR DEFAULT 'WHITE' I_SPEED TYPE ZSPEED OPTIONAL, DISPLAY_ATTRIBUTES, ACCELERATE IMPORTING I_DSPEED TYPE ZSPEED, DECELERATE IMPORTING I_DSPEED TYPE ZSPEED, GET_COLOR RETURNING VALUE(R_COLOR) TYPE ZCOLOR, SET_COLOR IMPORTING I_COLOR TYPE ZCOLOR.


10 | P a g e CLASS-METHODS: DISPLAY_NUMVEHICLES, CLASS_CONSTRUCTOR. PRIVATE SECTION. DATA: COLOR TYPE ZCOLOR, SPEED TYPE ZSPEED. CLASS-DATA: NUMVEHICLES TYPE I. ENDCLASS. CLASS LCL_VEHICLE IMPLEMENTATION. METHOD CLASS_CONSTRUCTOR. NUMVEHICLES = SY-UZEIT+4(2). ENDMETHOD. METHOD CONSTRUCTOR. DATA: REGNO TYPE I. ME->REGNO = I_REGNO. COLOR = I_COLOR. SPEED = I_SPEED. ADD 1 TO NUMVEHICLES. ENDMETHOD. METHOD DISPLAY_NUMVEHICLES. WRITE: / 'Number of vehicles on the road =', NUMVEHICLES. * WRITE: / 'REGNO =', REGNO. ENDMETHOD. METHOD DISPLAY_ATTRIBUTES. WRITE: / 'Attributes of vehicle:', 'REGNO =', REGNO, 'COLOR =', COLOR, 'SPEED =', SPEED. ENDMETHOD. METHOD ACCELERATE. ADD I_DSPEED TO SPEED. ENDMETHOD. METHOD DECELERATE. IF I_DSPEED GT SPEED. WRITE: / 'Unable to decelerate below zero'. EXIT. ENDIF. SUBTRACT I_DSPEED FROM SPEED. ENDMETHOD. METHOD GET_COLOR. R_COLOR = COLOR. ENDMETHOD. METHOD SET_COLOR. COLOR = I_COLOR. ENDMETHOD. ENDCLASS.

" SPEED = SPEED - I_DSPEED.


11 | P a g e DATA: GO_VEHICLE1 GO_VEHICLE2 GO_VEHICLE3 GO_VEHICLE4

TYPE TYPE TYPE TYPE

REF REF REF REF

TO TO TO TO

LCL_VEHICLE, LCL_VEHICLE, LCL_VEHICLE, LCL_VEHICLE.

START-OF-SELECTION. * * *

GO_VEHICLE1->DISPLAY_NUMVEHICLES( ). GO_VEHICLE2->DISPLAY_NUMVEHICLES( ). GO_VEHICLE3->DISPLAY_NUMVEHICLES( ). LCL_VEHICLE=>DISPLAY_NUMVEHICLES( ). CREATE OBJECT GO_VEHICLE1 EXPORTING I_REGNO = 'AAA1111' I_COLOR = 'RED' I_SPEED = 50. CALL METHOD GO_VEHICLE1->DISPLAY_ATTRIBUTES. CREATE OBJECT GO_VEHICLE2 EXPORTING I_REGNO = 'BBB2222' I_COLOR = 'GREEN' I_SPEED = 90. GO_VEHICLE2->DISPLAY_ATTRIBUTES( ). CREATE OBJECT GO_VEHICLE3 EXPORTING I_REGNO = 'CCC3333' I_COLOR = 'BLUE' I_SPEED = 20. GO_VEHICLE3->DISPLAY_ATTRIBUTES( ). CREATE OBJECT GO_VEHICLE4 EXPORTING I_REGNO = 'DDD4444'. GO_VEHICLE4->DISPLAY_ATTRIBUTES( ). LCL_VEHICLE=>DISPLAY_NUMVEHICLES( ).

*&---------------------------------------------------------------------* *& Report Z00_OBJ03_VEHICLE1 *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

Z00_OBJ08_AGGREGATION.

CLASS LCL_VEHICLE DEFINITION. PUBLIC SECTION. DATA: REGNO TYPE ZREGNO READ-ONLY. METHODS: CONSTRUCTOR IMPORTING I_REGNO TYPE ZREGNO I_COLOR TYPE ZCOLOR DEFAULT 'WHITE'


12 | P a g e I_SPEED TYPE ZSPEED OPTIONAL, DISPLAY_ATTRIBUTES, ACCELERATE IMPORTING I_DSPEED TYPE ZSPEED, DECELERATE IMPORTING I_DSPEED TYPE ZSPEED, GET_COLOR RETURNING VALUE(R_COLOR) TYPE ZCOLOR, SET_COLOR IMPORTING I_COLOR TYPE ZCOLOR. CLASS-METHODS: DISPLAY_NUMVEHICLES, CLASS_CONSTRUCTOR. PRIVATE SECTION. DATA: COLOR TYPE ZCOLOR, SPEED TYPE ZSPEED. CLASS-DATA: NUMVEHICLES TYPE I. ENDCLASS. CLASS LCL_VEHICLE IMPLEMENTATION. METHOD CLASS_CONSTRUCTOR. NUMVEHICLES = SY-UZEIT+4(2). ENDMETHOD. METHOD CONSTRUCTOR. DATA: REGNO TYPE I. ME->REGNO = I_REGNO. COLOR = I_COLOR. SPEED = I_SPEED. ADD 1 TO NUMVEHICLES. ENDMETHOD. METHOD DISPLAY_NUMVEHICLES. WRITE: / 'Number of vehicles on the road =', NUMVEHICLES. * WRITE: / 'REGNO =', REGNO. ENDMETHOD. METHOD DISPLAY_ATTRIBUTES. WRITE: / 'Attributes of vehicle:', 'REGNO =', REGNO, 'COLOR =', COLOR, 'SPEED =', SPEED. ENDMETHOD. METHOD ACCELERATE. ADD I_DSPEED TO SPEED. ENDMETHOD. METHOD DECELERATE. IF I_DSPEED GT SPEED. WRITE: / 'Unable to decelerate below zero'. EXIT. ENDIF. SUBTRACT I_DSPEED FROM SPEED.

" SPEED = SPEED - I_DSPEED.


13 | P a g e ENDMETHOD. METHOD GET_COLOR. R_COLOR = COLOR. ENDMETHOD. METHOD SET_COLOR. COLOR = I_COLOR. ENDMETHOD. ENDCLASS. CLASS LCL_ROAD DEFINITION. PUBLIC SECTION. METHODS: CONSTRUCTOR IMPORTING I_NAME TYPE STRING, ADD_VEHICLE IMPORTING I_VEHICLE TYPE REF TO LCL_VEHICLE, REMOVE_VEHICLE IMPORTING I_REGNO TYPE ZREGNO, GET_VEHICLE IMPORTING I_REGNO TYPE ZREGNO RETURNING VALUE(RO_VEHICLE) TYPE REF TO LCL_VEHICLE, DISPLAY_ATTRIBUTES. PRIVATE SECTION. DATA: NAME TYPE STRING, IT_VEHICLE TYPE TABLE OF REF TO LCL_VEHICLE. ENDCLASS. CLASS LCL_ROAD IMPLEMENTATION. METHOD CONSTRUCTOR. NAME = I_NAME. ENDMETHOD. METHOD REMOVE_VEHICLE. DELETE IT_VEHICLE WHERE TABLE_LINE->REGNO = I_REGNO. ENDMETHOD. METHOD GET_VEHICLE. READ TABLE IT_VEHICLE INTO RO_VEHICLE WITH KEY TABLE_LINE->REGNO = I_REGNO. ENDMETHOD. METHOD ADD_VEHICLE. APPEND I_VEHICLE TO IT_VEHICLE. ENDMETHOD. METHOD DISPLAY_ATTRIBUTES. DATA: LO_VEHICLE TYPE REF TO LCL_VEHICLE. DATA: LO_VEHICLE LIKE LINE OF IT_VEHICLE. WRITE: / 'Road name =', NAME. LOOP AT IT_VEHICLE INTO LO_VEHICLE. LO_VEHICLE->DISPLAY_ATTRIBUTES( ). ENDLOOP. ENDMETHOD. ENDCLASS. *


14 | P a g e

DATA: GO_VEHICLE GO_ROAD

TYPE REF TO LCL_VEHICLE, TYPE REF TO LCL_ROAD.

START-OF-SELECTION. LCL_VEHICLE=>DISPLAY_NUMVEHICLES( ). CREATE OBJECT GO_ROAD EXPORTING I_NAME = 'Jalan Sehala'. CREATE OBJECT GO_VEHICLE EXPORTING I_REGNO = 'AAA1111' I_COLOR = 'RED' I_SPEED = 50. GO_ROAD->ADD_VEHICLE( GO_VEHICLE ). CREATE OBJECT GO_VEHICLE EXPORTING I_REGNO = 'BBB2222' I_COLOR = 'GREEN' I_SPEED = 90. GO_ROAD->ADD_VEHICLE( GO_VEHICLE ). CREATE OBJECT GO_VEHICLE EXPORTING I_REGNO = 'CCC3333' I_COLOR = 'BLUE' I_SPEED = 20. GO_ROAD->ADD_VEHICLE( GO_VEHICLE ). CREATE OBJECT GO_VEHICLE EXPORTING I_REGNO = 'DDD4444'. GO_ROAD->ADD_VEHICLE( GO_VEHICLE ). GO_ROAD->DISPLAY_ATTRIBUTES( ). LCL_VEHICLE=>DISPLAY_NUMVEHICLES( ). ULINE. GO_ROAD->REMOVE_VEHICLE( 'CCC3333' ). GO_ROAD->DISPLAY_ATTRIBUTES( ). ULINE. GO_VEHICLE = GO_ROAD->GET_VEHICLE( 'BBB2222' ). GO_VEHICLE->DISPLAY_ATTRIBUTES( ). *

GO_ROAD->GET_VEHICLE( 'BBB2222' )->DISPLAY_ATTRIBUTES( ).

*&---------------------------------------------------------------------* *& Report Z00_OBJ03_VEHICLE1 *&


15 | P a g e *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

Z00_OBJ09_INHERITANCE.

TYPE-POOLS: LIST. CLASS LCL_VEHICLE DEFINITION. PUBLIC SECTION. DATA: REGNO TYPE ZREGNO READ-ONLY. METHODS: CONSTRUCTOR IMPORTING I_REGNO TYPE ZREGNO I_COLOR TYPE ZCOLOR DEFAULT 'WHITE' I_SPEED TYPE ZSPEED OPTIONAL, DISPLAY_ATTRIBUTES, ACCELERATE IMPORTING I_DSPEED TYPE ZSPEED, DECELERATE IMPORTING I_DSPEED TYPE ZSPEED, GET_COLOR RETURNING VALUE(R_COLOR) TYPE ZCOLOR, SET_COLOR IMPORTING I_COLOR TYPE ZCOLOR. CLASS-METHODS: DISPLAY_NUMVEHICLES, CLASS_CONSTRUCTOR. PROTECTED SECTION. DATA: COLOR TYPE ZCOLOR, SPEED TYPE ZSPEED. PRIVATE SECTION. CLASS-DATA: NUMVEHICLES TYPE I. ENDCLASS. CLASS LCL_VEHICLE IMPLEMENTATION. METHOD CLASS_CONSTRUCTOR. NUMVEHICLES = SY-UZEIT+4(2). ENDMETHOD. METHOD CONSTRUCTOR. DATA: REGNO TYPE I. ME->REGNO = I_REGNO. COLOR = I_COLOR. SPEED = I_SPEED. ADD 1 TO NUMVEHICLES. ENDMETHOD. METHOD DISPLAY_NUMVEHICLES. WRITE: / 'Number of vehicles on the road =', NUMVEHICLES. * WRITE: / 'REGNO =', REGNO. ENDMETHOD. METHOD DISPLAY_ATTRIBUTES.


16 | P a g e WRITE: / 'Attributes of vehicle:', 'REGNO =', REGNO, 'COLOR =', COLOR, 'SPEED =', SPEED. ENDMETHOD. METHOD ACCELERATE. ADD I_DSPEED TO SPEED. ENDMETHOD. METHOD DECELERATE. IF I_DSPEED GT SPEED. WRITE: / 'Unable to decelerate below zero'. EXIT. ENDIF. SUBTRACT I_DSPEED FROM SPEED. ENDMETHOD.

" SPEED = SPEED - I_DSPEED.

METHOD GET_COLOR. R_COLOR = COLOR. ENDMETHOD. METHOD SET_COLOR. COLOR = I_COLOR. ENDMETHOD. ENDCLASS.

CLASS LCL_CAR DEFINITION INHERITING FROM LCL_VEHICLE. PUBLIC SECTION. METHODS: GET_NUMPASS RETURNING VALUE(R_NUMPASS) TYPE ZNUMPASS, SET_NUMPASS IMPORTING I_NUMPASS TYPE ZNUMPASS, DISPLAY_ATTRIBUTES REDEFINITION, CONSTRUCTOR IMPORTING I_REGNO TYPE ZREGNO I_COLOR TYPE ZCOLOR DEFAULT 'WHITE' I_SPEED TYPE ZSPEED OPTIONAL I_NUMPASS TYPE ZNUMPASS. PRIVATE SECTION. DATA: NUMPASS TYPE ZNUMPASS. ENDCLASS. CLASS LCL_CAR IMPLEMENTATION. METHOD CONSTRUCTOR. * REGNO = I_REGNO. * COLOR = I_COLOR. * SPEED = I_SPEED. * ADD 1 TO NUMVEHICLES. SUPER->CONSTRUCTOR( I_REGNO = I_REGNO I_COLOR = I_COLOR I_SPEED = I_SPEED ). NUMPASS = I_NUMPASS.


17 | P a g e ENDMETHOD. METHOD GET_NUMPASS. R_NUMPASS = NUMPASS. ENDMETHOD. METHOD SET_NUMPASS. NUMPASS = I_NUMPASS. ENDMETHOD. METHOD DISPLAY_ATTRIBUTES. SUPER->DISPLAY_ATTRIBUTES( ). WRITE: / 'Attributes of', ICON_CAR AS ICON, 'REGNO =', REGNO, 'COLOR =', COLOR, 'SPEED =', SPEED. WRITE: 'NUMPASS =', NUMPASS. ENDMETHOD. ENDCLASS. *

CLASS LCL_TRUCK DEFINITION INHERITING FROM LCL_VEHICLE. PUBLIC SECTION. METHODS: DISPLAY_ATTRIBUTES REDEFINITION, CONSTRUCTOR IMPORTING I_REGNO TYPE ZREGNO I_COLOR TYPE ZCOLOR DEFAULT 'WHITE' I_SPEED TYPE ZSPEED OPTIONAL I_CARGO TYPE ZCARGO. PRIVATE SECTION. DATA: CARGO TYPE ZCARGO. ENDCLASS. CLASS LCL_TRUCK IMPLEMENTATION. METHOD DISPLAY_ATTRIBUTES. WRITE: / 'Attributes of ', ICON_WS_TRUCK AS ICON, 'REGNO =', REGNO, 'COLOR =', COLOR, 'SPEED =', SPEED, 'CARGO =', CARGO. ENDMETHOD. METHOD CONSTRUCTOR. SUPER->CONSTRUCTOR( I_REGNO = I_REGNO I_COLOR = I_COLOR I_SPEED = I_SPEED ). CARGO = I_CARGO. ENDMETHOD. ENDCLASS.

CLASS LCL_ROAD DEFINITION. PUBLIC SECTION. METHODS: CONSTRUCTOR


18 | P a g e IMPORTING I_NAME TYPE STRING, ADD_VEHICLE IMPORTING I_VEHICLE TYPE REF TO LCL_VEHICLE, REMOVE_VEHICLE IMPORTING I_REGNO TYPE ZREGNO, GET_VEHICLE IMPORTING I_REGNO TYPE ZREGNO RETURNING VALUE(RO_VEHICLE) TYPE REF TO LCL_VEHICLE, DISPLAY_ATTRIBUTES. PRIVATE SECTION. DATA: NAME TYPE STRING, IT_VEHICLE TYPE TABLE OF REF TO LCL_VEHICLE. ENDCLASS. CLASS LCL_ROAD IMPLEMENTATION. METHOD CONSTRUCTOR. NAME = I_NAME. ENDMETHOD. METHOD REMOVE_VEHICLE. DELETE IT_VEHICLE WHERE TABLE_LINE->REGNO = I_REGNO. ENDMETHOD. METHOD GET_VEHICLE. READ TABLE IT_VEHICLE INTO RO_VEHICLE WITH KEY TABLE_LINE->REGNO = I_REGNO. ENDMETHOD. METHOD ADD_VEHICLE. APPEND I_VEHICLE TO IT_VEHICLE. ENDMETHOD. METHOD DISPLAY_ATTRIBUTES. DATA: LO_VEHICLE TYPE REF TO LCL_VEHICLE. DATA: LO_VEHICLE LIKE LINE OF IT_VEHICLE. WRITE: / 'Road name =', NAME. LOOP AT IT_VEHICLE INTO LO_VEHICLE. LO_VEHICLE->DISPLAY_ATTRIBUTES( ). ENDLOOP. ENDMETHOD. ENDCLASS. *

DATA: GO_VEHICLE GO_CAR GO_TRUCK GO_ROAD

TYPE TYPE TYPE TYPE

REF REF REF REF

TO TO TO TO

LCL_VEHICLE, LCL_CAR, LCL_TRUCK, LCL_ROAD.

START-OF-SELECTION. LCL_VEHICLE=>DISPLAY_NUMVEHICLES( ). CREATE OBJECT GO_ROAD EXPORTING I_NAME = 'Jalan Sehala'.


19 | P a g e CREATE OBJECT GO_CAR EXPORTING I_REGNO = I_COLOR = I_SPEED = I_NUMPASS * GO_CAR->SET_NUMPASS( GO_ROAD->ADD_VEHICLE(

'AAA1111' 'RED' 50 = 3. 3 ). GO_CAR ).

CREATE OBJECT GO_TRUCK EXPORTING I_REGNO = 'BBB2222' I_COLOR = 'GREEN' I_SPEED = 90 I_CARGO = 10000. GO_ROAD->ADD_VEHICLE( GO_TRUCK ). CREATE OBJECT GO_CAR EXPORTING I_REGNO = 'CCC3333' I_COLOR = 'BLUE' I_SPEED = 20 I_NUMPASS = 2. * GO_CAR->SET_NUMPASS( 2 ). GO_ROAD->ADD_VEHICLE( GO_CAR ). CREATE OBJECT GO_VEHICLE EXPORTING I_REGNO = 'DDD4444'. GO_ROAD->ADD_VEHICLE( GO_VEHICLE ). GO_ROAD->DISPLAY_ATTRIBUTES( ). LCL_VEHICLE=>DISPLAY_NUMVEHICLES( ). *&---------------------------------------------------------------------* *& Report Z00_OBJ03_VEHICLE1 *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

Z00_OBJ10_CASTING.

TYPE-POOLS: LIST. CLASS LCL_VEHICLE DEFINITION. PUBLIC SECTION. DATA: REGNO TYPE ZREGNO READ-ONLY. METHODS: CONSTRUCTOR IMPORTING I_REGNO TYPE ZREGNO I_COLOR TYPE ZCOLOR DEFAULT 'WHITE' I_SPEED TYPE ZSPEED OPTIONAL, DISPLAY_ATTRIBUTES, ACCELERATE IMPORTING I_DSPEED TYPE ZSPEED, DECELERATE


20 | P a g e IMPORTING I_DSPEED TYPE ZSPEED, GET_COLOR RETURNING VALUE(R_COLOR) TYPE ZCOLOR, SET_COLOR IMPORTING I_COLOR TYPE ZCOLOR. CLASS-METHODS: DISPLAY_NUMVEHICLES, CLASS_CONSTRUCTOR. PROTECTED SECTION. DATA: COLOR TYPE ZCOLOR, SPEED TYPE ZSPEED. PRIVATE SECTION. CLASS-DATA: NUMVEHICLES TYPE I. ENDCLASS. CLASS LCL_VEHICLE IMPLEMENTATION. METHOD CLASS_CONSTRUCTOR. NUMVEHICLES = SY-UZEIT+4(2). ENDMETHOD. METHOD CONSTRUCTOR. DATA: REGNO TYPE I. ME->REGNO = I_REGNO. COLOR = I_COLOR. SPEED = I_SPEED. ADD 1 TO NUMVEHICLES. ENDMETHOD. METHOD DISPLAY_NUMVEHICLES. WRITE: / 'Number of vehicles on the road =', NUMVEHICLES. * WRITE: / 'REGNO =', REGNO. ENDMETHOD. METHOD DISPLAY_ATTRIBUTES. WRITE: / 'Attributes of vehicle:', 'REGNO =', REGNO, 'COLOR =', COLOR, 'SPEED =', SPEED. ENDMETHOD. METHOD ACCELERATE. ADD I_DSPEED TO SPEED. ENDMETHOD. METHOD DECELERATE. IF I_DSPEED GT SPEED. WRITE: / 'Unable to decelerate below zero'. EXIT. ENDIF. SUBTRACT I_DSPEED FROM SPEED. ENDMETHOD. METHOD GET_COLOR. R_COLOR = COLOR.

" SPEED = SPEED - I_DSPEED.


21 | P a g e ENDMETHOD. METHOD SET_COLOR. COLOR = I_COLOR. ENDMETHOD. ENDCLASS.

CLASS LCL_CAR DEFINITION INHERITING FROM LCL_VEHICLE. PUBLIC SECTION. METHODS: GET_NUMPASS RETURNING VALUE(R_NUMPASS) TYPE ZNUMPASS, SET_NUMPASS IMPORTING I_NUMPASS TYPE ZNUMPASS, DISPLAY_ATTRIBUTES REDEFINITION, CONSTRUCTOR IMPORTING I_REGNO TYPE ZREGNO I_COLOR TYPE ZCOLOR DEFAULT 'WHITE' I_SPEED TYPE ZSPEED OPTIONAL I_NUMPASS TYPE ZNUMPASS. PRIVATE SECTION. DATA: NUMPASS TYPE ZNUMPASS. ENDCLASS. CLASS LCL_CAR IMPLEMENTATION. METHOD CONSTRUCTOR. * REGNO = I_REGNO. * COLOR = I_COLOR. * SPEED = I_SPEED. * ADD 1 TO NUMVEHICLES. SUPER->CONSTRUCTOR( I_REGNO = I_REGNO I_COLOR = I_COLOR I_SPEED = I_SPEED ). NUMPASS = I_NUMPASS. ENDMETHOD. METHOD GET_NUMPASS. R_NUMPASS = NUMPASS. ENDMETHOD. METHOD SET_NUMPASS. NUMPASS = I_NUMPASS. ENDMETHOD. METHOD DISPLAY_ATTRIBUTES. * SUPER->DISPLAY_ATTRIBUTES( ). WRITE: / 'Attributes of', ICON_CAR AS ICON, 'REGNO =', REGNO, 'COLOR =', COLOR, 'SPEED =', SPEED. WRITE: 'NUMPASS =', NUMPASS. ENDMETHOD. ENDCLASS. CLASS LCL_TRUCK DEFINITION INHERITING FROM LCL_VEHICLE.


22 | P a g e PUBLIC SECTION. METHODS: DISPLAY_ATTRIBUTES REDEFINITION, CONSTRUCTOR IMPORTING I_REGNO TYPE ZREGNO I_COLOR TYPE ZCOLOR DEFAULT 'WHITE' I_SPEED TYPE ZSPEED OPTIONAL I_CARGO TYPE ZCARGO. PRIVATE SECTION. DATA: CARGO TYPE ZCARGO. ENDCLASS. CLASS LCL_TRUCK IMPLEMENTATION. METHOD DISPLAY_ATTRIBUTES. WRITE: / 'Attributes of ', ICON_WS_TRUCK AS ICON, 'REGNO =', REGNO, 'COLOR =', COLOR, 'SPEED =', SPEED, 'CARGO =', CARGO. ENDMETHOD. METHOD CONSTRUCTOR. SUPER->CONSTRUCTOR( I_REGNO = I_REGNO I_COLOR = I_COLOR I_SPEED = I_SPEED ). CARGO = I_CARGO. ENDMETHOD. ENDCLASS.

CLASS LCL_ROAD DEFINITION. PUBLIC SECTION. METHODS: CONSTRUCTOR IMPORTING I_NAME TYPE STRING, ADD_VEHICLE IMPORTING I_VEHICLE TYPE REF TO LCL_VEHICLE, REMOVE_VEHICLE IMPORTING I_REGNO TYPE ZREGNO, GET_VEHICLE IMPORTING I_REGNO TYPE ZREGNO RETURNING VALUE(RO_VEHICLE) TYPE REF TO LCL_VEHICLE, DISPLAY_ATTRIBUTES. PRIVATE SECTION. DATA: NAME TYPE STRING, IT_VEHICLE TYPE TABLE OF REF TO LCL_VEHICLE. ENDCLASS. CLASS LCL_ROAD IMPLEMENTATION. METHOD CONSTRUCTOR. NAME = I_NAME. ENDMETHOD. METHOD REMOVE_VEHICLE.


23 | P a g e DELETE IT_VEHICLE WHERE TABLE_LINE->REGNO = I_REGNO. ENDMETHOD. METHOD GET_VEHICLE. READ TABLE IT_VEHICLE INTO RO_VEHICLE WITH KEY TABLE_LINE->REGNO = I_REGNO. ENDMETHOD. METHOD ADD_VEHICLE. APPEND I_VEHICLE TO IT_VEHICLE. ENDMETHOD. METHOD DISPLAY_ATTRIBUTES. DATA: LO_VEHICLE TYPE REF TO LCL_VEHICLE. DATA: LO_VEHICLE LIKE LINE OF IT_VEHICLE. WRITE: / 'Road name =', NAME. LOOP AT IT_VEHICLE INTO LO_VEHICLE. LO_VEHICLE->DISPLAY_ATTRIBUTES( ). ENDLOOP. ENDMETHOD. ENDCLASS. *

DATA: GO_VEHICLE GO_CAR GO_TRUCK GO_ROAD

TYPE TYPE TYPE TYPE

REF REF REF REF

TO TO TO TO

LCL_VEHICLE, LCL_CAR, LCL_TRUCK, LCL_ROAD.

START-OF-SELECTION. CREATE OBJECT GO_CAR EXPORTING I_REGNO = I_COLOR = I_SPEED = I_NUMPASS CREATE OBJECT EXPORTING I_REGNO = I_COLOR = I_SPEED = I_CARGO =

'AAA1111' 'RED' 50 = 3.

GO_TRUCK 'BBB2222' 'GREEN' 90 10000.

* Up-cast * Done implicitly. GO_VEHICLE ?= GO_CAR. * Always allowed. Does not cause runtime errors. GO_VEHICLE = GO_TRUCK. * GO_VEHICLE = GO_CAR. GO_VEHICLE->DISPLAY_ATTRIBUTES( ). * Down cast * Must be done explicitly. * Might cause runtime errors. TRY. GO_CAR ?= GO_VEHICLE.


24 | P a g e GO_CAR->DISPLAY_ATTRIBUTES( ). CATCH CX_SY_MOVE_CAST_ERROR. WRITE: / 'Casting error'. ENDTRY.

* * * * * * * * * * ** * * * * * * * * * * * * * * * * ** * * * * * * * * * *

LCL_VEHICLE=>DISPLAY_NUMVEHICLES( ). CREATE OBJECT GO_ROAD EXPORTING I_NAME = 'Jalan Sehala'. CREATE OBJECT GO_CAR EXPORTING I_REGNO = I_COLOR = I_SPEED = I_NUMPASS GO_CAR->SET_NUMPASS( GO_ROAD->ADD_VEHICLE(

'AAA1111' 'RED' 50 = 3. 3 ). GO_CAR ).

CREATE OBJECT GO_TRUCK EXPORTING I_REGNO = 'BBB2222' I_COLOR = 'GREEN' I_SPEED = 90 I_CARGO = 10000. GO_ROAD->ADD_VEHICLE( GO_TRUCK ). CREATE OBJECT GO_CAR EXPORTING I_REGNO = 'CCC3333' I_COLOR = 'BLUE' I_SPEED = 20 I_NUMPASS = 2. GO_CAR->SET_NUMPASS( 2 ). GO_ROAD->ADD_VEHICLE( GO_CAR ). CREATE OBJECT GO_VEHICLE EXPORTING I_REGNO = 'DDD4444'. GO_ROAD->ADD_VEHICLE( GO_VEHICLE ). GO_ROAD->DISPLAY_ATTRIBUTES( ). LCL_VEHICLE=>DISPLAY_NUMVEHICLES( ).

*&---------------------------------------------------------------------* *& Report Z00_OBJ03_VEHICLE1 *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

Z00_OBJ11_EVENTS.

TYPE-POOLS: LIST.


25 | P a g e CLASS LCL_VEHICLE DEFINITION. PUBLIC SECTION. DATA: REGNO TYPE ZREGNO READ-ONLY. METHODS: CONSTRUCTOR IMPORTING I_REGNO TYPE ZREGNO I_COLOR TYPE ZCOLOR DEFAULT 'WHITE' I_SPEED TYPE ZSPEED OPTIONAL, DISPLAY_ATTRIBUTES, ACCELERATE IMPORTING I_DSPEED TYPE ZSPEED, DECELERATE IMPORTING I_DSPEED TYPE ZSPEED, GET_COLOR RETURNING VALUE(R_COLOR) TYPE ZCOLOR, SET_COLOR IMPORTING I_COLOR TYPE ZCOLOR. CLASS-METHODS: DISPLAY_NUMVEHICLES, CLASS_CONSTRUCTOR. EVENTS: SPEEDING EXPORTING VALUE(E_REGNO) TYPE ZREGNO VALUE(E_SPEED) TYPE ZSPEED. PROTECTED SECTION. DATA: COLOR TYPE ZCOLOR, SPEED TYPE ZSPEED. PRIVATE SECTION. CLASS-DATA: NUMVEHICLES TYPE I. ENDCLASS. CLASS LCL_VEHICLE IMPLEMENTATION. METHOD CLASS_CONSTRUCTOR. NUMVEHICLES = SY-UZEIT+4(2). ENDMETHOD. METHOD CONSTRUCTOR. DATA: REGNO TYPE I. ME->REGNO = I_REGNO. COLOR = I_COLOR. SPEED = I_SPEED. ADD 1 TO NUMVEHICLES. ENDMETHOD. METHOD DISPLAY_NUMVEHICLES. WRITE: / 'Number of vehicles on the road =', NUMVEHICLES. * WRITE: / 'REGNO =', REGNO. ENDMETHOD. METHOD DISPLAY_ATTRIBUTES. WRITE: / 'Attributes of vehicle:', 'REGNO =', REGNO, 'COLOR =', COLOR, 'SPEED =', SPEED. ENDMETHOD.


26 | P a g e METHOD ACCELERATE. ADD I_DSPEED TO SPEED. IF SPEED GT 110. RAISE EVENT SPEEDING EXPORTING E_REGNO = REGNO E_SPEED = SPEED. ENDIF. ENDMETHOD. METHOD DECELERATE. IF I_DSPEED GT SPEED. WRITE: / 'Unable to decelerate below zero'. EXIT. ENDIF. SUBTRACT I_DSPEED FROM SPEED. ENDMETHOD.

" SPEED = SPEED - I_DSPEED.

METHOD GET_COLOR. R_COLOR = COLOR. ENDMETHOD. METHOD SET_COLOR. COLOR = I_COLOR. ENDMETHOD. ENDCLASS.

CLASS LCL_CAR DEFINITION INHERITING FROM LCL_VEHICLE. PUBLIC SECTION. METHODS: GET_NUMPASS RETURNING VALUE(R_NUMPASS) TYPE ZNUMPASS, SET_NUMPASS IMPORTING I_NUMPASS TYPE ZNUMPASS, DISPLAY_ATTRIBUTES REDEFINITION, CONSTRUCTOR IMPORTING I_REGNO TYPE ZREGNO I_COLOR TYPE ZCOLOR DEFAULT 'WHITE' I_SPEED TYPE ZSPEED OPTIONAL I_NUMPASS TYPE ZNUMPASS. PRIVATE SECTION. DATA: NUMPASS TYPE ZNUMPASS. ENDCLASS. CLASS LCL_CAR IMPLEMENTATION. METHOD CONSTRUCTOR. * REGNO = I_REGNO. * COLOR = I_COLOR. * SPEED = I_SPEED. * ADD 1 TO NUMVEHICLES. SUPER->CONSTRUCTOR( I_REGNO = I_REGNO I_COLOR = I_COLOR I_SPEED = I_SPEED ). NUMPASS = I_NUMPASS. ENDMETHOD.


27 | P a g e METHOD GET_NUMPASS. R_NUMPASS = NUMPASS. ENDMETHOD. METHOD SET_NUMPASS. NUMPASS = I_NUMPASS. ENDMETHOD. METHOD DISPLAY_ATTRIBUTES. SUPER->DISPLAY_ATTRIBUTES( ). WRITE: / 'Attributes of', ICON_CAR AS ICON, 'REGNO =', REGNO, 'COLOR =', COLOR, 'SPEED =', SPEED. WRITE: 'NUMPASS =', NUMPASS. ENDMETHOD. ENDCLASS. *

CLASS LCL_TRUCK DEFINITION INHERITING FROM LCL_VEHICLE. PUBLIC SECTION. METHODS: DISPLAY_ATTRIBUTES REDEFINITION, CONSTRUCTOR IMPORTING I_REGNO TYPE ZREGNO I_COLOR TYPE ZCOLOR DEFAULT 'WHITE' I_SPEED TYPE ZSPEED OPTIONAL I_CARGO TYPE ZCARGO. PRIVATE SECTION. DATA: CARGO TYPE ZCARGO. ENDCLASS. CLASS LCL_TRUCK IMPLEMENTATION. METHOD DISPLAY_ATTRIBUTES. WRITE: / 'Attributes of ', ICON_WS_TRUCK AS ICON, 'REGNO =', REGNO, 'COLOR =', COLOR, 'SPEED =', SPEED, 'CARGO =', CARGO. ENDMETHOD. METHOD CONSTRUCTOR. SUPER->CONSTRUCTOR( I_REGNO = I_REGNO I_COLOR = I_COLOR I_SPEED = I_SPEED ). CARGO = I_CARGO. ENDMETHOD. ENDCLASS.

CLASS LCL_ROAD DEFINITION. PUBLIC SECTION. METHODS: CONSTRUCTOR IMPORTING I_NAME TYPE STRING,


28 | P a g e ADD_VEHICLE IMPORTING I_VEHICLE TYPE REF TO LCL_VEHICLE, REMOVE_VEHICLE IMPORTING I_REGNO TYPE ZREGNO, GET_VEHICLE IMPORTING I_REGNO TYPE ZREGNO RETURNING VALUE(RO_VEHICLE) TYPE REF TO LCL_VEHICLE, DISPLAY_ATTRIBUTES. PRIVATE SECTION. DATA: NAME TYPE STRING, IT_VEHICLE TYPE TABLE OF REF TO LCL_VEHICLE. ENDCLASS. CLASS LCL_ROAD IMPLEMENTATION. METHOD CONSTRUCTOR. NAME = I_NAME. ENDMETHOD. METHOD REMOVE_VEHICLE. DELETE IT_VEHICLE WHERE TABLE_LINE->REGNO = I_REGNO. ENDMETHOD. METHOD GET_VEHICLE. READ TABLE IT_VEHICLE INTO RO_VEHICLE WITH KEY TABLE_LINE->REGNO = I_REGNO. ENDMETHOD. METHOD ADD_VEHICLE. APPEND I_VEHICLE TO IT_VEHICLE. ENDMETHOD. METHOD DISPLAY_ATTRIBUTES. DATA: LO_VEHICLE TYPE REF TO LCL_VEHICLE. DATA: LO_VEHICLE LIKE LINE OF IT_VEHICLE. WRITE: / 'Road name =', NAME. LOOP AT IT_VEHICLE INTO LO_VEHICLE. LO_VEHICLE->DISPLAY_ATTRIBUTES( ). ENDLOOP. ENDMETHOD. ENDCLASS. *

CLASS LCL_POLICE DEFINITION. PUBLIC SECTION. METHODS: GOTO_WORK, GO_HOME.

PRIVATE SECTION. METHODS: FINE FOR EVENT SPEEDING OF LCL_VEHICLE IMPORTING E_REGNO E_SPEED. ENDCLASS. CLASS LCL_POLICE IMPLEMENTATION.


29 | P a g e METHOD FINE. WRITE: / 'Police caught vehicle REGNO =', E_REGNO, 'speeding at SPEED =', E_SPEED. ENDMETHOD. METHOD GOTO_WORK. SET HANDLER FINE FOR ALL INSTANCES. * ACTIVATION 'X'. ENDMETHOD. METHOD GO_HOME. SET HANDLER FINE FOR ALL INSTANCES ACTIVATION SPACE. " ' '. ENDMETHOD. ENDCLASS.

CLASS LCL_FATHER DEFINITION. PUBLIC SECTION. METHODS: CONSTRUCTOR. PRIVATE SECTION. METHODS: SCOLD FOR EVENT SPEEDING OF LCL_VEHICLE IMPORTING SENDER. ENDCLASS. CLASS LCL_FATHER IMPLEMENTATION. METHOD CONSTRUCTOR. SET HANDLER SCOLD FOR ALL INSTANCES. ENDMETHOD. METHOD SCOLD. WRITE: / 'Father is scolding the following vehicle:'. SENDER->DISPLAY_ATTRIBUTES( ). ENDMETHOD. ENDCLASS.

DATA: GO_VEHICLE GO_CAR GO_TRUCK GO_ROAD GO_POLICE GO_FATHER GT_VEHICLE

TYPE TYPE TYPE TYPE TYPE TYPE TYPE

REF TO LCL_VEHICLE, REF TO LCL_CAR, REF TO LCL_TRUCK, REF TO LCL_ROAD, REF TO LCL_POLICE, REF TO LCL_FATHER, TABLE OF REF TO LCL_VEHICLE.

START-OF-SELECTION. CREATE OBJECT GO_POLICE. GO_POLICE->GOTO_WORK( ). * GO_POLICE->GO_HOME( ). * SET HANDLER GO_POLICE->FINE FOR ALL INSTANCES ACTIVATION SPACE. CREATE OBJECT GO_FATHER. CLEAR GO_POLICE. CLEAR GO_FATHER.


30 | P a g e CREATE OBJECT GO_CAR EXPORTING I_REGNO = I_COLOR = I_SPEED = I_NUMPASS

'AAA1111' 'RED' 50 = 3.

APPEND GO_CAR TO GT_VEHICLE. CREATE OBJECT EXPORTING I_REGNO = I_COLOR = I_SPEED = I_CARGO =

GO_TRUCK 'BBB2222' 'GREEN' 90 10000.

APPEND GO_TRUCK TO GT_VEHICLE. CREATE OBJECT EXPORTING I_REGNO = I_COLOR = I_SPEED = I_NUMPASS

GO_CAR 'CCC3333' 'BLUE' 20 = 2.

APPEND GO_CAR TO GT_VEHICLE. CREATE OBJECT GO_VEHICLE EXPORTING I_REGNO = 'DDD4444'. APPEND GO_VEHICLE TO GT_VEHICLE. LOOP AT GT_VEHICLE INTO GO_VEHICLE. GO_VEHICLE->DISPLAY_ATTRIBUTES( ). ENDLOOP. ULINE. WRITE: / 'Accelerating vehicles by 70'. LOOP AT GT_VEHICLE INTO GO_VEHICLE. GO_VEHICLE->ACCELERATE( 70 ). ENDLOOP. ULINE. LOOP AT GT_VEHICLE INTO GO_VEHICLE. GO_VEHICLE->DISPLAY_ATTRIBUTES( ). ENDLOOP.

*&---------------------------------------------------------------------* *& Report Z00_OBJ03_VEHICLE1 *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------*


31 | P a g e REPORT

Z00_OBJ12_TEST_GLOBAL_CLASS.

DATA: GO_VEHICLE GO_CAR GO_TRUCK * GO_ROAD GO_POLICE GO_FATHER GT_VEHICLE

TYPE REF TO ZCL_VEHICLE00, TYPE REF TO ZCL_CAR00, TYPE REF TO ZCL_TRUCK00, TYPE REF TO ZCL_ROAD00, TYPE REF TO ZCL_POLICE00, TYPE REF TO ZCL_FATHER00, TYPE TABLE OF REF TO ZCL_VEHICLE00.

START-OF-SELECTION. CREATE OBJECT GO_POLICE. GO_POLICE->GOTO_WORK( ). *

CREATE OBJECT GO_FATHER. GO_FATHER = ZCL_FATHER00=>FATHER. CREATE OBJECT GO_CAR EXPORTING I_REGNO = I_COLOR = I_SPEED = I_NUMPASS

'AAA1111' 'RED' 50 = 3.

APPEND GO_CAR TO GT_VEHICLE. CREATE OBJECT EXPORTING I_REGNO = I_COLOR = I_SPEED = I_CARGO =

GO_TRUCK 'BBB2222' 'GREEN' 90 10000.

APPEND GO_TRUCK TO GT_VEHICLE. CREATE OBJECT EXPORTING I_REGNO = I_COLOR = I_SPEED = I_NUMPASS

GO_CAR 'CCC3333' 'BLUE' 20 = 2.

APPEND GO_CAR TO GT_VEHICLE. CREATE OBJECT GO_CAR EXPORTING I_REGNO = 'DDD4444' I_NUMPASS = 3. APPEND GO_CAR TO GT_VEHICLE. LOOP AT GT_VEHICLE INTO GO_VEHICLE. GO_VEHICLE->DISPLAY_ATTRIBUTES( ). ENDLOOP. ULINE.


32 | P a g e WRITE: / 'Accelerating vehicles by 70'. LOOP AT GT_VEHICLE INTO GO_VEHICLE. GO_VEHICLE->ACCELERATE( 70 ). ENDLOOP. ULINE. LOOP AT GT_VEHICLE INTO GO_VEHICLE. GO_VEHICLE->DISPLAY_ATTRIBUTES( ). ENDLOOP.

*&---------------------------------------------------------------------* *& Report Z00_OBJ13_INTERFACE1 *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

Z00_OBJ13_INTERFACE1.

INTERFACE LIF_OCEANITEM. METHODS: SWIM. ENDINTERFACE. INTERFACE LIF_SKYITEM. METHODS: FLY, LAND. ENDINTERFACE. CLASS VEHICLE DEFINITION. ENDCLASS. CLASS CAR DEFINITION INHERITING FROM VEHICLE. ENDCLASS. CLASS BOAT DEFINITION INHERITING FROM VEHICLE. PUBLIC SECTION. INTERFACES: LIF_OCEANITEM. ALIASES: XXX FOR LIF_OCEANITEM~SWIM. ENDCLASS. CLASS BOAT IMPLEMENTATION. * METHOD LIF_OCEANITEM~SWIM. METHOD XXX. WRITE: / 'Boat is sailing in the ocean...'. ENDMETHOD. ENDCLASS.

CLASS AIRPLANE DEFINITION INHERITING FROM VEHICLE. PUBLIC SECTION. INTERFACES: LIF_SKYITEM. ENDCLASS. CLASS AIRPLANE IMPLEMENTATION.


33 | P a g e METHOD LIF_SKYITEM~FLY. WRITE: / 'Airplane is firing its jet engines...'. ENDMETHOD. METHOD LIF_SKYITEM~LAND. WRITE: / 'Airplane landing gears coming down...'. ENDMETHOD. ENDCLASS. CLASS SEAPLANE DEFINITION INHERITING FROM AIRPLANE. PUBLIC SECTION. INTERFACES: LIF_OCEANITEM. METHODS: LIF_SKYITEM~FLY REDEFINITION, LIF_SKYITEM~LAND REDEFINITION. ENDCLASS. CLASS SEAPLANE IMPLEMENTATION. METHOD LIF_OCEANITEM~SWIM. WRITE: / 'Seaplane is floating...'. ENDMETHOD. METHOD LIF_SKYITEM~FLY. WRITE: / 'Seaplane is spinning its propellers...'. ENDMETHOD. METHOD LIF_SKYITEM~LAND. WRITE: / 'Seaplane is landing on the sea...'. ENDMETHOD. ENDCLASS.

CLASS ANIMAL DEFINITION. ENDCLASS. CLASS BIRD DEFINITION INHERITING FROM ANIMAL. PUBLIC SECTION. INTERFACES: LIF_SKYITEM. ENDCLASS. CLASS BIRD IMPLEMENTATION. METHOD LIF_SKYITEM~FLY. WRITE: / 'Bird is flapping its wings...'. ENDMETHOD. METHOD LIF_SKYITEM~LAND. WRITE: / 'Bird is landing on the branch...'. ENDMETHOD. ENDCLASS.

CLASS FISH DEFINITION INHERITING FROM ANIMAL. PUBLIC SECTION. INTERFACES: LIF_OCEANITEM. ENDCLASS.


34 | P a g e CLASS FISH IMPLEMENTATION. METHOD LIF_OCEANITEM~SWIM. WRITE: / 'Fish is swimming in the ocean...'. ENDMETHOD. ENDCLASS.

CLASS CAT DEFINITION INHERITING FROM ANIMAL. ENDCLASS.

CLASS LCL_OCEAN DEFINITION. PUBLIC SECTION. METHODS: ADD_OCEANSTUFF IMPORTING I_OCEANSTUFF TYPE REF TO LIF_OCEANITEM, DISPLAY_ATTRIBUTES. PRIVATE SECTION. DATA: IT_OCEANSTUFF TYPE TABLE OF REF TO LIF_OCEANITEM. ENDCLASS. CLASS LCL_OCEAN IMPLEMENTATION. METHOD ADD_OCEANSTUFF. APPEND I_OCEANSTUFF TO IT_OCEANSTUFF. ENDMETHOD. METHOD DISPLAY_ATTRIBUTES. DATA: LO_OCEANSTUFF TYPE REF TO LIF_OCEANITEM. WRITE: / 'Things found in the ocean...'. LOOP AT IT_OCEANSTUFF INTO LO_OCEANSTUFF. LO_OCEANSTUFF->SWIM( ). ENDLOOP. ENDMETHOD. ENDCLASS. CLASS LCL_SKY DEFINITION. PUBLIC SECTION. METHODS: ADD_SKYSTUFF IMPORTING I_SKYSTUFF TYPE REF TO LIF_SKYITEM, DISPLAY_ATTRIBUTES. PRIVATE SECTION. DATA: IT_SKYSTUFF TYPE TABLE OF REF TO LIF_SKYITEM. ENDCLASS. CLASS LCL_SKY IMPLEMENTATION. METHOD ADD_SKYSTUFF. APPEND I_SKYSTUFF TO IT_SKYSTUFF. ENDMETHOD. METHOD DISPLAY_ATTRIBUTES. DATA: LO_SKYSTUFF TYPE REF TO LIF_SKYITEM. WRITE: / 'Things found in the sky...'. LOOP AT IT_SKYSTUFF INTO LO_SKYSTUFF. LO_SKYSTUFF->FLY( ). LO_SKYSTUFF->LAND( ). ENDLOOP. ENDMETHOD. ENDCLASS.


35 | P a g e

DATA: GO_VEHICLE GO_CAR GO_BOAT GO_AIRPLANE GO_SEAPLANE GO_ANIMAL GO_CAT GO_FISH GO_BIRD GO_OCEANITEM GO_SKYITEM GO_OCEAN GO_SKY

TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE

REF REF REF REF REF REF REF REF REF REF REF REF REF

TO TO TO TO TO TO TO TO TO TO TO TO TO

VEHICLE, CAR, BOAT, AIRPLANE, SEAPLANE, ANIMAL, CAT, FISH, BIRD, LIF_OCEANITEM, LIF_SKYITEM, LCL_OCEAN, LCL_SKY.

START-OF-SELECTION. CREATE CREATE CREATE CREATE CREATE CREATE CREATE

OBJECT OBJECT OBJECT OBJECT OBJECT OBJECT OBJECT

GO_CAR. GO_BOAT. GO_AIRPLANE. GO_SEAPLANE. GO_CAT. GO_FISH. GO_BIRD.

CREATE OBJECT GO_OCEAN. CREATE OBJECT GO_SKY. GO_OCEAN->ADD_OCEANSTUFF( GO_BOAT ). GO_OCEAN->ADD_OCEANSTUFF( GO_SEAPLANE ). GO_OCEAN->ADD_OCEANSTUFF( GO_FISH ). GO_SKY->ADD_SKYSTUFF( GO_AIRPLANE ). GO_SKY->ADD_SKYSTUFF( GO_SEAPLANE ). GO_SKY->ADD_SKYSTUFF( GO_BIRD ). GO_OCEAN->DISPLAY_ATTRIBUTES( ). ULINE. GO_SKY->DISPLAY_ATTRIBUTES( ). ULINE. *

GO_BOAT->SWIM( ). GO_BOAT->LIF_OCEANITEM~SWIM( ).

* Up cast GO_OCEANITEM = GO_BOAT. GO_OCEANITEM->SWIM( ). GO_BOAT->XXX( ).

*&---------------------------------------------------------------------* *& Report Z00_OBJ14_INTERFACE_TEST *& *&---------------------------------------------------------------------* *&


36 | P a g e *& *&---------------------------------------------------------------------* REPORT

Z00_OBJ14_INTERFACE_TEST.

DATA: GO_VEHICLE GO_CAR GO_BOAT GO_OCEANITEM

TYPE TYPE TYPE TYPE

REF REF REF REF

TO TO TO TO

ZCL_VEHICLE00, ZCL_CAR00, ZCL_BOAT00, ZIF_OCEANITEM00.

START-OF-SELECTION. CREATE OBJECT GO_BOAT EXPORTING I_REGNO = 'AAA1111' * I_COLOR = 'White' * I_SPEED = . CREATE OBJECT GO_CAR EXPORTING I_REGNO = 'BBB2222' * I_COLOR = 'WHITE' * I_SPEED = I_NUMPASS = 3 . GO_BOAT->ZIF_OCEANITEM00~SWIM( ). GO_BOAT->ZIF_OCEANITEM00~SWIM( ). GO_BOAT->SWIM( ).

*&---------------------------------------------------------------------* *& Report Z00_OBJ03_VEHICLE1 *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

Z00_OBJ15_SPECIAL_OO_TECHNIQUE.

TYPE-POOLS: LIST. CLASS LCL_FATHER DEFINITION DEFERRED. CLASS LCL_VEHICLE DEFINITION ABSTRACT FRIENDS LCL_FATHER. PUBLIC SECTION. DATA: REGNO TYPE ZREGNO READ-ONLY. METHODS: CONSTRUCTOR IMPORTING I_REGNO TYPE ZREGNO I_COLOR TYPE ZCOLOR DEFAULT 'WHITE' I_SPEED TYPE ZSPEED OPTIONAL, DISPLAY_ATTRIBUTES ABSTRACT, ACCELERATE IMPORTING I_DSPEED TYPE ZSPEED, DECELERATE FINAL


37 | P a g e IMPORTING I_DSPEED TYPE ZSPEED, GET_COLOR RETURNING VALUE(R_COLOR) TYPE ZCOLOR, SET_COLOR IMPORTING I_COLOR TYPE ZCOLOR. CLASS-METHODS: DISPLAY_NUMVEHICLES, CLASS_CONSTRUCTOR. EVENTS: SPEEDING EXPORTING VALUE(E_REGNO) TYPE ZREGNO VALUE(E_SPEED) TYPE ZSPEED. PROTECTED SECTION. DATA: COLOR TYPE ZCOLOR, SPEED TYPE ZSPEED. PRIVATE SECTION. CLASS-DATA: NUMVEHICLES TYPE I. ENDCLASS. CLASS LCL_VEHICLE IMPLEMENTATION. METHOD CLASS_CONSTRUCTOR. NUMVEHICLES = SY-UZEIT+4(2). ENDMETHOD. METHOD CONSTRUCTOR. DATA: REGNO TYPE I. ME->REGNO = I_REGNO. COLOR = I_COLOR. SPEED = I_SPEED. ADD 1 TO NUMVEHICLES. ENDMETHOD. METHOD DISPLAY_NUMVEHICLES. WRITE: / 'Number of vehicles on the road =', NUMVEHICLES. * WRITE: / 'REGNO =', REGNO. ENDMETHOD. * * * * * *

METHOD DISPLAY_ATTRIBUTES. WRITE: / 'Attributes of vehicle:', 'REGNO =', REGNO, 'COLOR =', COLOR, 'SPEED =', SPEED. ENDMETHOD. METHOD ACCELERATE. ADD I_DSPEED TO SPEED. IF SPEED GT 110. RAISE EVENT SPEEDING EXPORTING E_REGNO = REGNO E_SPEED = SPEED. ENDIF. ENDMETHOD. METHOD DECELERATE. IF I_DSPEED GT SPEED. WRITE: / 'Unable to decelerate below zero'.


38 | P a g e EXIT. ENDIF. SUBTRACT I_DSPEED FROM SPEED. ENDMETHOD.

" SPEED = SPEED - I_DSPEED.

METHOD GET_COLOR. R_COLOR = COLOR. ENDMETHOD. METHOD SET_COLOR. COLOR = I_COLOR. ENDMETHOD. ENDCLASS.

CLASS LCL_CAR DEFINITION INHERITING FROM LCL_VEHICLE FINAL. PUBLIC SECTION. METHODS: GET_NUMPASS RETURNING VALUE(R_NUMPASS) TYPE ZNUMPASS, SET_NUMPASS IMPORTING I_NUMPASS TYPE ZNUMPASS, DISPLAY_ATTRIBUTES REDEFINITION, CONSTRUCTOR IMPORTING I_REGNO TYPE ZREGNO I_COLOR TYPE ZCOLOR DEFAULT 'WHITE' I_SPEED TYPE ZSPEED OPTIONAL I_NUMPASS TYPE ZNUMPASS. PRIVATE SECTION. DATA: NUMPASS TYPE ZNUMPASS. ENDCLASS. CLASS LCL_CAR IMPLEMENTATION. METHOD CONSTRUCTOR. * REGNO = I_REGNO. * COLOR = I_COLOR. * SPEED = I_SPEED. * ADD 1 TO NUMVEHICLES. SUPER->CONSTRUCTOR( I_REGNO = I_REGNO I_COLOR = I_COLOR I_SPEED = I_SPEED ). NUMPASS = I_NUMPASS. ENDMETHOD. METHOD GET_NUMPASS. R_NUMPASS = NUMPASS. ENDMETHOD. METHOD SET_NUMPASS. NUMPASS = I_NUMPASS. ENDMETHOD. METHOD DISPLAY_ATTRIBUTES. * SUPER->DISPLAY_ATTRIBUTES( ). WRITE: / 'Attributes of', ICON_CAR AS ICON,


39 | P a g e 'REGNO 'COLOR 'SPEED WRITE: 'NUMPASS ENDMETHOD. ENDCLASS.

=', =', =', =',

REGNO, COLOR, SPEED. NUMPASS.

CLASS LCL_TRUCK DEFINITION INHERITING FROM LCL_VEHICLE FINAL. PUBLIC SECTION. METHODS: DISPLAY_ATTRIBUTES REDEFINITION, CONSTRUCTOR IMPORTING I_REGNO TYPE ZREGNO I_COLOR TYPE ZCOLOR DEFAULT 'WHITE' I_SPEED TYPE ZSPEED OPTIONAL I_CARGO TYPE ZCARGO. PRIVATE SECTION. DATA: CARGO TYPE ZCARGO. ENDCLASS. CLASS LCL_TRUCK IMPLEMENTATION. METHOD DISPLAY_ATTRIBUTES. WRITE: / 'Attributes of ', ICON_WS_TRUCK AS ICON, 'REGNO =', REGNO, 'COLOR =', COLOR, 'SPEED =', SPEED, 'CARGO =', CARGO. ENDMETHOD. METHOD CONSTRUCTOR. SUPER->CONSTRUCTOR( I_REGNO = I_REGNO I_COLOR = I_COLOR I_SPEED = I_SPEED ). CARGO = I_CARGO. ENDMETHOD. ENDCLASS.

CLASS LCL_ROAD DEFINITION FINAL. PUBLIC SECTION. METHODS: CONSTRUCTOR IMPORTING I_NAME TYPE STRING, ADD_VEHICLE IMPORTING I_VEHICLE TYPE REF TO LCL_VEHICLE, REMOVE_VEHICLE IMPORTING I_REGNO TYPE ZREGNO, GET_VEHICLE IMPORTING I_REGNO TYPE ZREGNO RETURNING VALUE(RO_VEHICLE) TYPE REF TO LCL_VEHICLE, DISPLAY_ATTRIBUTES. PRIVATE SECTION. DATA: NAME TYPE STRING, IT_VEHICLE TYPE TABLE OF REF TO LCL_VEHICLE.


40 | P a g e ENDCLASS. CLASS LCL_ROAD IMPLEMENTATION. METHOD CONSTRUCTOR. NAME = I_NAME. ENDMETHOD. METHOD REMOVE_VEHICLE. DELETE IT_VEHICLE WHERE TABLE_LINE->REGNO = I_REGNO. ENDMETHOD. METHOD GET_VEHICLE. READ TABLE IT_VEHICLE INTO RO_VEHICLE WITH KEY TABLE_LINE->REGNO = I_REGNO. ENDMETHOD. METHOD ADD_VEHICLE. APPEND I_VEHICLE TO IT_VEHICLE. ENDMETHOD. METHOD DISPLAY_ATTRIBUTES. DATA: LO_VEHICLE TYPE REF TO LCL_VEHICLE. DATA: LO_VEHICLE LIKE LINE OF IT_VEHICLE. WRITE: / 'Road name =', NAME. LOOP AT IT_VEHICLE INTO LO_VEHICLE. LO_VEHICLE->DISPLAY_ATTRIBUTES( ). ENDLOOP. ENDMETHOD. ENDCLASS. *

CLASS LCL_POLICE DEFINITION FINAL. PUBLIC SECTION. METHODS: GOTO_WORK, GO_HOME.

PRIVATE SECTION. METHODS: FINE FOR EVENT SPEEDING OF LCL_VEHICLE IMPORTING E_REGNO E_SPEED. ENDCLASS. CLASS LCL_POLICE IMPLEMENTATION. METHOD FINE. WRITE: / 'Police caught vehicle REGNO =', E_REGNO, 'speeding at SPEED =', E_SPEED. ENDMETHOD. METHOD GOTO_WORK. SET HANDLER FINE FOR ALL INSTANCES. * ACTIVATION 'X'. ENDMETHOD. METHOD GO_HOME.


41 | P a g e SET HANDLER FINE FOR ALL INSTANCES ACTIVATION SPACE. " ' '. ENDMETHOD. ENDCLASS.

CLASS LCL_FATHER DEFINITION FINAL CREATE PRIVATE. PUBLIC SECTION. CLASS-DATA: FATHER TYPE REF TO LCL_FATHER READ-ONLY. CLASS-METHODS: CLASS_CONSTRUCTOR. METHODS: CONSTRUCTOR. PRIVATE SECTION. METHODS: SCOLD FOR EVENT SPEEDING OF LCL_VEHICLE IMPORTING SENDER. ENDCLASS. CLASS LCL_FATHER IMPLEMENTATION. METHOD CLASS_CONSTRUCTOR. CREATE OBJECT FATHER. ENDMETHOD. METHOD CONSTRUCTOR. SET HANDLER SCOLD FOR ALL INSTANCES. ENDMETHOD. METHOD SCOLD. WRITE: / 'Father is scolding the following vehicle:'. SENDER->DISPLAY_ATTRIBUTES( ). WRITE: / 'Father is scolding vehicle', 'REGNO =', SENDER->REGNO, 'COLOR =', SENDER->COLOR, 'SPEED =', SENDER->SPEED, 'NUMVEHICLES =', LCL_VEHICLE=>NUMVEHICLES. ENDMETHOD. ENDCLASS. * *

DATA: GO_VEHICLE GO_CAR GO_TRUCK GO_ROAD GO_POLICE GO_FATHER GT_VEHICLE

TYPE TYPE TYPE TYPE TYPE TYPE TYPE

REF TO LCL_VEHICLE, REF TO LCL_CAR, REF TO LCL_TRUCK, REF TO LCL_ROAD, REF TO LCL_POLICE, REF TO LCL_FATHER, TABLE OF REF TO LCL_VEHICLE.

START-OF-SELECTION. CREATE OBJECT GO_POLICE. GO_POLICE->GOTO_WORK( ). * GO_POLICE->GO_HOME( ). * SET HANDLER GO_POLICE->FINE FOR ALL INSTANCES ACTIVATION SPACE. * * *

CREATE OBJECT GO_FATHER. CREATE OBJECT GO_FATHER. CREATE OBJECT GO_FATHER. GO_FATHER = LCL_FATHER=>FATHER. GO_FATHER = LCL_FATHER=>FATHER.


42 | P a g e GO_FATHER = LCL_FATHER=>FATHER.

CREATE OBJECT GO_CAR EXPORTING I_REGNO = I_COLOR = I_SPEED = I_NUMPASS

'AAA1111' 'RED' 50 = 3.

APPEND GO_CAR TO GT_VEHICLE. CREATE OBJECT EXPORTING I_REGNO = I_COLOR = I_SPEED = I_CARGO =

GO_TRUCK 'BBB2222' 'GREEN' 90 10000.

APPEND GO_TRUCK TO GT_VEHICLE. CREATE OBJECT EXPORTING I_REGNO = I_COLOR = I_SPEED = I_NUMPASS

GO_CAR 'CCC3333' 'BLUE' 20 = 2.

APPEND GO_CAR TO GT_VEHICLE. CREATE OBJECT GO_CAR EXPORTING I_REGNO = 'DDD4444' I_NUMPASS = 1. APPEND GO_CAR TO GT_VEHICLE. LOOP AT GT_VEHICLE INTO GO_VEHICLE. GO_VEHICLE->DISPLAY_ATTRIBUTES( ). ENDLOOP. ULINE. WRITE: / 'Accelerating vehicles by 70'. LOOP AT GT_VEHICLE INTO GO_VEHICLE. GO_VEHICLE->ACCELERATE( 70 ). ENDLOOP. ULINE. LOOP AT GT_VEHICLE INTO GO_VEHICLE. GO_VEHICLE->DISPLAY_ATTRIBUTES( ). ENDLOOP.

*&---------------------------------------------------------------------* *& Report Z00_OBJ03_VEHICLE1 *&


43 | P a g e *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

Z00_OBJ16_OLD_EXCEPTIONS.

TYPE-POOLS: LIST. CLASS LCL_FATHER DEFINITION DEFERRED. CLASS LCL_VEHICLE DEFINITION ABSTRACT FRIENDS LCL_FATHER. PUBLIC SECTION. DATA: REGNO TYPE ZREGNO READ-ONLY. METHODS: CONSTRUCTOR IMPORTING I_REGNO TYPE ZREGNO I_COLOR TYPE ZCOLOR DEFAULT 'WHITE' I_SPEED TYPE ZSPEED OPTIONAL, DISPLAY_ATTRIBUTES ABSTRACT, ACCELERATE IMPORTING I_DSPEED TYPE ZSPEED, DECELERATE FINAL IMPORTING I_DSPEED TYPE ZSPEED EXCEPTIONS INVALID_SPEED INVALID_DSPEED, GET_COLOR RETURNING VALUE(R_COLOR) TYPE ZCOLOR, SET_COLOR IMPORTING I_COLOR TYPE ZCOLOR. CLASS-METHODS: DISPLAY_NUMVEHICLES, CLASS_CONSTRUCTOR. EVENTS: SPEEDING EXPORTING VALUE(E_REGNO) TYPE ZREGNO VALUE(E_SPEED) TYPE ZSPEED. PROTECTED SECTION. DATA: COLOR TYPE ZCOLOR, SPEED TYPE ZSPEED. PRIVATE SECTION. CLASS-DATA: NUMVEHICLES TYPE I. ENDCLASS. CLASS LCL_VEHICLE IMPLEMENTATION. METHOD CLASS_CONSTRUCTOR. NUMVEHICLES = SY-UZEIT+4(2). ENDMETHOD. METHOD CONSTRUCTOR. DATA: REGNO TYPE I. ME->REGNO = I_REGNO. COLOR = I_COLOR. SPEED = I_SPEED. ADD 1 TO NUMVEHICLES. ENDMETHOD.


44 | P a g e METHOD DISPLAY_NUMVEHICLES. WRITE: / 'Number of vehicles on the road =', NUMVEHICLES. * WRITE: / 'REGNO =', REGNO. ENDMETHOD. * * * * * *

METHOD DISPLAY_ATTRIBUTES. WRITE: / 'Attributes of vehicle:', 'REGNO =', REGNO, 'COLOR =', COLOR, 'SPEED =', SPEED. ENDMETHOD. METHOD ACCELERATE. ADD I_DSPEED TO SPEED. IF SPEED GT 110. RAISE EVENT SPEEDING EXPORTING E_REGNO = REGNO E_SPEED = SPEED. ENDIF. ENDMETHOD. METHOD DECELERATE. IF I_DSPEED LT 0. RAISE INVALID_DSPEED. ENDIF. IF I_DSPEED GT SPEED. RAISE INVALID_SPEED. ENDIF. SUBTRACT I_DSPEED FROM SPEED. ENDMETHOD.

" SPEED = SPEED - I_DSPEED.

METHOD GET_COLOR. R_COLOR = COLOR. ENDMETHOD. METHOD SET_COLOR. COLOR = I_COLOR. ENDMETHOD. ENDCLASS.

CLASS LCL_CAR DEFINITION INHERITING FROM LCL_VEHICLE FINAL. PUBLIC SECTION. METHODS: GET_NUMPASS RETURNING VALUE(R_NUMPASS) TYPE ZNUMPASS, SET_NUMPASS IMPORTING I_NUMPASS TYPE ZNUMPASS, DISPLAY_ATTRIBUTES REDEFINITION, CONSTRUCTOR IMPORTING I_REGNO TYPE ZREGNO I_COLOR TYPE ZCOLOR DEFAULT 'WHITE' I_SPEED TYPE ZSPEED OPTIONAL I_NUMPASS TYPE ZNUMPASS. PRIVATE SECTION. DATA: NUMPASS TYPE ZNUMPASS.


45 | P a g e ENDCLASS. CLASS LCL_CAR IMPLEMENTATION. METHOD CONSTRUCTOR. * REGNO = I_REGNO. * COLOR = I_COLOR. * SPEED = I_SPEED. * ADD 1 TO NUMVEHICLES. SUPER->CONSTRUCTOR( I_REGNO = I_REGNO I_COLOR = I_COLOR I_SPEED = I_SPEED ). NUMPASS = I_NUMPASS. ENDMETHOD. METHOD GET_NUMPASS. R_NUMPASS = NUMPASS. ENDMETHOD. METHOD SET_NUMPASS. NUMPASS = I_NUMPASS. ENDMETHOD. METHOD DISPLAY_ATTRIBUTES. * SUPER->DISPLAY_ATTRIBUTES( ). WRITE: / 'Attributes of', ICON_CAR AS ICON, 'REGNO =', REGNO, 'COLOR =', COLOR, 'SPEED =', SPEED. WRITE: 'NUMPASS =', NUMPASS. ENDMETHOD. ENDCLASS. CLASS LCL_TRUCK DEFINITION INHERITING FROM LCL_VEHICLE FINAL. PUBLIC SECTION. METHODS: DISPLAY_ATTRIBUTES REDEFINITION, CONSTRUCTOR IMPORTING I_REGNO TYPE ZREGNO I_COLOR TYPE ZCOLOR DEFAULT 'WHITE' I_SPEED TYPE ZSPEED OPTIONAL I_CARGO TYPE ZCARGO. PRIVATE SECTION. DATA: CARGO TYPE ZCARGO. ENDCLASS. CLASS LCL_TRUCK IMPLEMENTATION. METHOD DISPLAY_ATTRIBUTES. WRITE: / 'Attributes of ', ICON_WS_TRUCK AS ICON, 'REGNO =', REGNO, 'COLOR =', COLOR, 'SPEED =', SPEED, 'CARGO =', CARGO. ENDMETHOD. METHOD CONSTRUCTOR. SUPER->CONSTRUCTOR( I_REGNO = I_REGNO


46 | P a g e I_COLOR = I_COLOR I_SPEED = I_SPEED ). CARGO = I_CARGO. ENDMETHOD. ENDCLASS.

CLASS LCL_ROAD DEFINITION FINAL. PUBLIC SECTION. METHODS: CONSTRUCTOR IMPORTING I_NAME TYPE STRING, ADD_VEHICLE IMPORTING I_VEHICLE TYPE REF TO LCL_VEHICLE, REMOVE_VEHICLE IMPORTING I_REGNO TYPE ZREGNO, GET_VEHICLE IMPORTING I_REGNO TYPE ZREGNO RETURNING VALUE(RO_VEHICLE) TYPE REF TO LCL_VEHICLE, DISPLAY_ATTRIBUTES. PRIVATE SECTION. DATA: NAME TYPE STRING, IT_VEHICLE TYPE TABLE OF REF TO LCL_VEHICLE. ENDCLASS. CLASS LCL_ROAD IMPLEMENTATION. METHOD CONSTRUCTOR. NAME = I_NAME. ENDMETHOD. METHOD REMOVE_VEHICLE. DELETE IT_VEHICLE WHERE TABLE_LINE->REGNO = I_REGNO. ENDMETHOD. METHOD GET_VEHICLE. READ TABLE IT_VEHICLE INTO RO_VEHICLE WITH KEY TABLE_LINE->REGNO = I_REGNO. ENDMETHOD. METHOD ADD_VEHICLE. APPEND I_VEHICLE TO IT_VEHICLE. ENDMETHOD. METHOD DISPLAY_ATTRIBUTES. DATA: LO_VEHICLE TYPE REF TO LCL_VEHICLE. DATA: LO_VEHICLE LIKE LINE OF IT_VEHICLE. WRITE: / 'Road name =', NAME. LOOP AT IT_VEHICLE INTO LO_VEHICLE. LO_VEHICLE->DISPLAY_ATTRIBUTES( ). ENDLOOP. ENDMETHOD. ENDCLASS. *


47 | P a g e CLASS LCL_POLICE DEFINITION FINAL. PUBLIC SECTION. METHODS: GOTO_WORK, GO_HOME.

PRIVATE SECTION. METHODS: FINE FOR EVENT SPEEDING OF LCL_VEHICLE IMPORTING E_REGNO E_SPEED. ENDCLASS. CLASS LCL_POLICE IMPLEMENTATION. METHOD FINE. WRITE: / 'Police caught vehicle REGNO =', E_REGNO, 'speeding at SPEED =', E_SPEED. ENDMETHOD. METHOD GOTO_WORK. SET HANDLER FINE FOR ALL INSTANCES. * ACTIVATION 'X'. ENDMETHOD. METHOD GO_HOME. SET HANDLER FINE FOR ALL INSTANCES ACTIVATION SPACE. " ' '. ENDMETHOD. ENDCLASS.

CLASS LCL_FATHER DEFINITION FINAL CREATE PRIVATE. PUBLIC SECTION. CLASS-DATA: FATHER TYPE REF TO LCL_FATHER READ-ONLY. CLASS-METHODS: CLASS_CONSTRUCTOR. METHODS: CONSTRUCTOR. PRIVATE SECTION. METHODS: SCOLD FOR EVENT SPEEDING OF LCL_VEHICLE IMPORTING SENDER. ENDCLASS. CLASS LCL_FATHER IMPLEMENTATION. METHOD CLASS_CONSTRUCTOR. CREATE OBJECT FATHER. ENDMETHOD. METHOD CONSTRUCTOR. SET HANDLER SCOLD FOR ALL INSTANCES. ENDMETHOD.

* *

METHOD SCOLD. WRITE: / 'Father is scolding the following vehicle:'. SENDER->DISPLAY_ATTRIBUTES( ). WRITE: / 'Father is scolding vehicle', 'REGNO =', SENDER->REGNO,


48 | P a g e 'COLOR =', SENDER->COLOR, 'SPEED =', SENDER->SPEED, 'NUMVEHICLES =', LCL_VEHICLE=>NUMVEHICLES. ENDMETHOD. ENDCLASS.

DATA: GO_VEHICLE GO_CAR GO_TRUCK GO_ROAD GO_POLICE GO_FATHER GT_VEHICLE

TYPE TYPE TYPE TYPE TYPE TYPE TYPE

REF TO LCL_VEHICLE, REF TO LCL_CAR, REF TO LCL_TRUCK, REF TO LCL_ROAD, REF TO LCL_POLICE, REF TO LCL_FATHER, TABLE OF REF TO LCL_VEHICLE.

START-OF-SELECTION. CREATE OBJECT GO_POLICE. GO_POLICE->GOTO_WORK( ). * GO_POLICE->GO_HOME( ). * SET HANDLER GO_POLICE->FINE FOR ALL INSTANCES ACTIVATION SPACE. * * *

CREATE OBJECT GO_FATHER. CREATE OBJECT GO_FATHER. CREATE OBJECT GO_FATHER. GO_FATHER = LCL_FATHER=>FATHER. GO_FATHER = LCL_FATHER=>FATHER. GO_FATHER = LCL_FATHER=>FATHER.

CREATE OBJECT GO_CAR EXPORTING I_REGNO = I_COLOR = I_SPEED = I_NUMPASS

'AAA1111' 'RED' 50 = 3.

APPEND GO_CAR TO GT_VEHICLE. CREATE OBJECT EXPORTING I_REGNO = I_COLOR = I_SPEED = I_CARGO =

GO_TRUCK 'BBB2222' 'GREEN' 90 10000.

APPEND GO_TRUCK TO GT_VEHICLE. CREATE OBJECT EXPORTING I_REGNO = I_COLOR = I_SPEED = I_NUMPASS

GO_CAR 'CCC3333' 'BLUE' 20 = 2.

APPEND GO_CAR TO GT_VEHICLE. CREATE OBJECT GO_CAR EXPORTING


49 | P a g e I_REGNO = 'DDD4444' I_NUMPASS = 1. APPEND GO_CAR TO GT_VEHICLE. LOOP AT GT_VEHICLE INTO GO_VEHICLE. GO_VEHICLE->DISPLAY_ATTRIBUTES( ). ENDLOOP. ULINE. WRITE: / 'Decelerating vehicles by 10'.

* * * * * *

LOOP AT GT_VEHICLE INTO GO_VEHICLE. GO_VEHICLE->DECELERATE( 10 ). CALL METHOD GO_VEHICLE->DECELERATE EXPORTING I_DSPEED = -10 EXCEPTIONS INVALID_SPEED = 1 OTHERS = 2. GO_VEHICLE->DECELERATE( EXPORTING I_DSPEED = 10 EXCEPTIONS INVALID_SPEED = 1 OTHERS = 2 ). CASE SY-SUBRC. WHEN 1. WRITE: / 'Invalid speed exception occurred'. WHEN 2. WRITE: / 'Oops... some other exception occurred'. ENDCASE. ENDLOOP. ULINE. LOOP AT GT_VEHICLE INTO GO_VEHICLE. GO_VEHICLE->DISPLAY_ATTRIBUTES( ). ENDLOOP.

*&---------------------------------------------------------------------* *& Report Z00_OBJ17_NEW_EXCEPTIONS *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

Z00_OBJ17_NEW_EXCEPTIONS.

PARAMETERS: PA_A TYPE I DEFAULT 2. PARAMETERS: PA_B TYPE I DEFAULT 2000. DATA: GV_RESULT TYPE I. DATA: GV_MSG TYPE STRING. DATA: GO_ARITHMETIC_OVERFLOW GO_ROOT

TYPE REF TO CX_SY_ARITHMETIC_OVERFLOW, TYPE REF TO CX_ROOT.


50 | P a g e

START-OF-SELECTION. TRY . GV_RESULT = PA_A * PA_B. WRITE: / 'GV_RESULT =', GV_RESULT. * CATCH CX_SY_ARITHMETIC_OVERFLOW INTO GO_ARITHMETIC_OVERFLOW. * GV_MSG = GO_ARITHMETIC_OVERFLOW->GET_TEXT( ). CATCH CX_SY_ARITHMETIC_OVERFLOW INTO GO_ROOT. GV_MSG = GO_ROOT->GET_TEXT( ). WRITE: / GV_MSG. CATCH CX_ROOT INTO GO_ROOT. GV_MSG = GO_ROOT->GET_TEXT( ). WRITE: / GV_MSG. ENDTRY.

*&---------------------------------------------------------------------* *& Report Z00_OBJ03_VEHICLE1 *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

Z00_OBJ18_CLASS_EXCEPTIONS.

TYPE-POOLS: LIST. CLASS LCL_FATHER DEFINITION DEFERRED. CLASS LCL_VEHICLE DEFINITION ABSTRACT FRIENDS LCL_FATHER. PUBLIC SECTION. DATA: REGNO TYPE ZREGNO READ-ONLY.

* *

METHODS: CONSTRUCTOR IMPORTING I_REGNO TYPE ZREGNO I_COLOR TYPE ZCOLOR DEFAULT 'WHITE' I_SPEED TYPE ZSPEED OPTIONAL, DISPLAY_ATTRIBUTES ABSTRACT, ACCELERATE IMPORTING I_DSPEED TYPE ZSPEED, DECELERATE FINAL IMPORTING I_DSPEED TYPE ZSPEED RAISING ZCX_INVALID_SPEED00, EXCEPTIONS INVALID_SPEED INVALID_DSPEED, GET_COLOR RETURNING VALUE(R_COLOR) TYPE ZCOLOR, SET_COLOR IMPORTING I_COLOR TYPE ZCOLOR. CLASS-METHODS: DISPLAY_NUMVEHICLES, CLASS_CONSTRUCTOR. EVENTS: SPEEDING EXPORTING VALUE(E_REGNO) TYPE ZREGNO VALUE(E_SPEED) TYPE ZSPEED.


51 | P a g e PROTECTED SECTION. DATA: COLOR TYPE ZCOLOR, SPEED TYPE ZSPEED. PRIVATE SECTION. CLASS-DATA: NUMVEHICLES TYPE I. ENDCLASS. CLASS LCL_VEHICLE IMPLEMENTATION. METHOD CLASS_CONSTRUCTOR. NUMVEHICLES = SY-UZEIT+4(2). ENDMETHOD. METHOD CONSTRUCTOR. DATA: REGNO TYPE I. ME->REGNO = I_REGNO. COLOR = I_COLOR. SPEED = I_SPEED. ADD 1 TO NUMVEHICLES. ENDMETHOD. METHOD DISPLAY_NUMVEHICLES. WRITE: / 'Number of vehicles on the road =', NUMVEHICLES. * WRITE: / 'REGNO =', REGNO. ENDMETHOD. * * * * * *

METHOD DISPLAY_ATTRIBUTES. WRITE: / 'Attributes of vehicle:', 'REGNO =', REGNO, 'COLOR =', COLOR, 'SPEED =', SPEED. ENDMETHOD. METHOD ACCELERATE. ADD I_DSPEED TO SPEED. IF SPEED GT 110. RAISE EVENT SPEEDING EXPORTING E_REGNO = REGNO E_SPEED = SPEED. ENDIF. ENDMETHOD.

METHOD DECELERATE. IF I_DSPEED GT SPEED. RAISE EXCEPTION TYPE ZCX_INVALID_SPEED00 EXPORTING TEXTID = ZCX_INVALID_SPEED00=>ZCX_INVALID_SPEED00 * PREVIOUS = REGNO = REGNO SPEED = SPEED DSPEED = I_DSPEED . ENDIF. SUBTRACT I_DSPEED FROM SPEED.

" SPEED = SPEED - I_DSPEED.


52 | P a g e ENDMETHOD. METHOD GET_COLOR. R_COLOR = COLOR. ENDMETHOD. METHOD SET_COLOR. COLOR = I_COLOR. ENDMETHOD. ENDCLASS.

CLASS LCL_CAR DEFINITION INHERITING FROM LCL_VEHICLE FINAL. PUBLIC SECTION. METHODS: GET_NUMPASS RETURNING VALUE(R_NUMPASS) TYPE ZNUMPASS, SET_NUMPASS IMPORTING I_NUMPASS TYPE ZNUMPASS, DISPLAY_ATTRIBUTES REDEFINITION, CONSTRUCTOR IMPORTING I_REGNO TYPE ZREGNO I_COLOR TYPE ZCOLOR DEFAULT 'WHITE' I_SPEED TYPE ZSPEED OPTIONAL I_NUMPASS TYPE ZNUMPASS. PRIVATE SECTION. DATA: NUMPASS TYPE ZNUMPASS. ENDCLASS. CLASS LCL_CAR IMPLEMENTATION. METHOD CONSTRUCTOR. * REGNO = I_REGNO. * COLOR = I_COLOR. * SPEED = I_SPEED. * ADD 1 TO NUMVEHICLES. SUPER->CONSTRUCTOR( I_REGNO = I_REGNO I_COLOR = I_COLOR I_SPEED = I_SPEED ). NUMPASS = I_NUMPASS. ENDMETHOD. METHOD GET_NUMPASS. R_NUMPASS = NUMPASS. ENDMETHOD. METHOD SET_NUMPASS. NUMPASS = I_NUMPASS. ENDMETHOD. METHOD DISPLAY_ATTRIBUTES. * SUPER->DISPLAY_ATTRIBUTES( ). WRITE: / 'Attributes of', ICON_CAR AS ICON, 'REGNO =', REGNO, 'COLOR =', COLOR, 'SPEED =', SPEED. WRITE: 'NUMPASS =', NUMPASS.


53 | P a g e ENDMETHOD. ENDCLASS. CLASS LCL_TRUCK DEFINITION INHERITING FROM LCL_VEHICLE FINAL. PUBLIC SECTION. METHODS: DISPLAY_ATTRIBUTES REDEFINITION, CONSTRUCTOR IMPORTING I_REGNO TYPE ZREGNO I_COLOR TYPE ZCOLOR DEFAULT 'WHITE' I_SPEED TYPE ZSPEED OPTIONAL I_CARGO TYPE ZCARGO. PRIVATE SECTION. DATA: CARGO TYPE ZCARGO. ENDCLASS. CLASS LCL_TRUCK IMPLEMENTATION. METHOD DISPLAY_ATTRIBUTES. WRITE: / 'Attributes of ', ICON_WS_TRUCK AS ICON, 'REGNO =', REGNO, 'COLOR =', COLOR, 'SPEED =', SPEED, 'CARGO =', CARGO. ENDMETHOD. METHOD CONSTRUCTOR. SUPER->CONSTRUCTOR( I_REGNO = I_REGNO I_COLOR = I_COLOR I_SPEED = I_SPEED ). CARGO = I_CARGO. ENDMETHOD. ENDCLASS.

CLASS LCL_ROAD DEFINITION FINAL. PUBLIC SECTION. METHODS: CONSTRUCTOR IMPORTING I_NAME TYPE STRING, ADD_VEHICLE IMPORTING I_VEHICLE TYPE REF TO LCL_VEHICLE, REMOVE_VEHICLE IMPORTING I_REGNO TYPE ZREGNO, GET_VEHICLE IMPORTING I_REGNO TYPE ZREGNO RETURNING VALUE(RO_VEHICLE) TYPE REF TO LCL_VEHICLE, DISPLAY_ATTRIBUTES. PRIVATE SECTION. DATA: NAME TYPE STRING, IT_VEHICLE TYPE TABLE OF REF TO LCL_VEHICLE. ENDCLASS. CLASS LCL_ROAD IMPLEMENTATION. METHOD CONSTRUCTOR.


54 | P a g e NAME = I_NAME. ENDMETHOD. METHOD REMOVE_VEHICLE. DELETE IT_VEHICLE WHERE TABLE_LINE->REGNO = I_REGNO. ENDMETHOD. METHOD GET_VEHICLE. READ TABLE IT_VEHICLE INTO RO_VEHICLE WITH KEY TABLE_LINE->REGNO = I_REGNO. ENDMETHOD. METHOD ADD_VEHICLE. APPEND I_VEHICLE TO IT_VEHICLE. ENDMETHOD. METHOD DISPLAY_ATTRIBUTES. DATA: LO_VEHICLE TYPE REF TO LCL_VEHICLE. DATA: LO_VEHICLE LIKE LINE OF IT_VEHICLE. WRITE: / 'Road name =', NAME. LOOP AT IT_VEHICLE INTO LO_VEHICLE. LO_VEHICLE->DISPLAY_ATTRIBUTES( ). ENDLOOP. ENDMETHOD. ENDCLASS. *

CLASS LCL_POLICE DEFINITION FINAL. PUBLIC SECTION. METHODS: GOTO_WORK, GO_HOME.

PRIVATE SECTION. METHODS: FINE FOR EVENT SPEEDING OF LCL_VEHICLE IMPORTING E_REGNO E_SPEED. ENDCLASS. CLASS LCL_POLICE IMPLEMENTATION. METHOD FINE. WRITE: / 'Police caught vehicle REGNO =', E_REGNO, 'speeding at SPEED =', E_SPEED. ENDMETHOD. METHOD GOTO_WORK. SET HANDLER FINE FOR ALL INSTANCES. * ACTIVATION 'X'. ENDMETHOD. METHOD GO_HOME. SET HANDLER FINE FOR ALL INSTANCES ACTIVATION SPACE. " ' '. ENDMETHOD. ENDCLASS.


55 | P a g e CLASS LCL_FATHER DEFINITION FINAL CREATE PRIVATE. PUBLIC SECTION. CLASS-DATA: FATHER TYPE REF TO LCL_FATHER READ-ONLY. CLASS-METHODS: CLASS_CONSTRUCTOR. METHODS: CONSTRUCTOR. PRIVATE SECTION. METHODS: SCOLD FOR EVENT SPEEDING OF LCL_VEHICLE IMPORTING SENDER. ENDCLASS. CLASS LCL_FATHER IMPLEMENTATION. METHOD CLASS_CONSTRUCTOR. CREATE OBJECT FATHER. ENDMETHOD. METHOD CONSTRUCTOR. SET HANDLER SCOLD FOR ALL INSTANCES. ENDMETHOD. METHOD SCOLD. WRITE: / 'Father is scolding the following vehicle:'. SENDER->DISPLAY_ATTRIBUTES( ). WRITE: / 'Father is scolding vehicle', 'REGNO =', SENDER->REGNO, 'COLOR =', SENDER->COLOR, 'SPEED =', SENDER->SPEED, 'NUMVEHICLES =', LCL_VEHICLE=>NUMVEHICLES. ENDMETHOD. ENDCLASS. * *

DATA: GO_VEHICLE GO_CAR GO_TRUCK GO_ROAD GO_POLICE GO_FATHER GT_VEHICLE GO_EXCEPTION GV_MSG

TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE

REF TO LCL_VEHICLE, REF TO LCL_CAR, REF TO LCL_TRUCK, REF TO LCL_ROAD, REF TO LCL_POLICE, REF TO LCL_FATHER, TABLE OF REF TO LCL_VEHICLE, REF TO CX_ROOT, STRING.

START-OF-SELECTION. CREATE OBJECT GO_POLICE. GO_POLICE->GOTO_WORK( ). * GO_POLICE->GO_HOME( ). * SET HANDLER GO_POLICE->FINE FOR ALL INSTANCES ACTIVATION SPACE. * * *

CREATE OBJECT GO_FATHER. CREATE OBJECT GO_FATHER. CREATE OBJECT GO_FATHER. GO_FATHER = LCL_FATHER=>FATHER. GO_FATHER = LCL_FATHER=>FATHER. GO_FATHER = LCL_FATHER=>FATHER.


56 | P a g e CREATE OBJECT GO_CAR EXPORTING I_REGNO = I_COLOR = I_SPEED = I_NUMPASS

'AAA1111' 'RED' 50 = 3.

APPEND GO_CAR TO GT_VEHICLE. CREATE OBJECT EXPORTING I_REGNO = I_COLOR = I_SPEED = I_CARGO =

GO_TRUCK 'BBB2222' 'GREEN' 90 10000.

APPEND GO_TRUCK TO GT_VEHICLE. CREATE OBJECT EXPORTING I_REGNO = I_COLOR = I_SPEED = I_NUMPASS

GO_CAR 'CCC3333' 'BLUE' 20 = 2.

APPEND GO_CAR TO GT_VEHICLE. CREATE OBJECT GO_CAR EXPORTING I_REGNO = 'DDD4444' I_NUMPASS = 1. APPEND GO_CAR TO GT_VEHICLE. LOOP AT GT_VEHICLE INTO GO_VEHICLE. GO_VEHICLE->DISPLAY_ATTRIBUTES( ). ENDLOOP. ULINE. WRITE: / 'Decelerating vehicles by 10'. LOOP AT GT_VEHICLE INTO GO_VEHICLE. TRY . GO_VEHICLE->DECELERATE( 10 ). CATCH ZCX_INVALID_SPEED00 INTO GO_EXCEPTION. GV_MSG = GO_EXCEPTION->GET_TEXT( ). WRITE: / 'EXCEPTION:', GV_MSG. ENDTRY. ENDLOOP. ULINE. LOOP AT GT_VEHICLE INTO GO_VEHICLE. GO_VEHICLE->DISPLAY_ATTRIBUTES( ). ENDLOOP.


57 | P a g e *&---------------------------------------------------------------------* *& Report Z00_OBJ19_TEST_EXCEPTIONS *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

Z00_OBJ19_TEST_EXCEPTIONS.

CLASS LCL_PROBLEM DEFINITION. PUBLIC SECTION. CLASS-METHODS: TEST_NO_CHECK, * RAISING ZCX_NO_CHECK, TEST_DYNAMIC_CHECK RAISING ZCX_DYNAMIC_CHECK, TEST_STATIC_CHECK RAISING ZCX_STATIC_CHECK. ENDCLASS. CLASS LCL_PROBLEM IMPLEMENTATION. METHOD TEST_NO_CHECK. WRITE: / 'TEST_NO_CHECK'. RAISE EXCEPTION TYPE ZCX_NO_CHECK. ENDMETHOD. METHOD TEST_DYNAMIC_CHECK. WRITE: / 'TEST_DYNAMIC_CHECK'. RAISE EXCEPTION TYPE ZCX_DYNAMIC_CHECK. ENDMETHOD. METHOD TEST_STATIC_CHECK. WRITE: / 'TEST_STATIC_CHECK'. RAISE EXCEPTION TYPE ZCX_STATIC_CHECK. ENDMETHOD. ENDCLASS. CLASS LCL_CALL_PROBLEM DEFINITION. PUBLIC SECTION. CLASS-METHODS: CALL_PROBLEM. ENDCLASS. CLASS LCL_CALL_PROBLEM IMPLEMENTATION. METHOD CALL_PROBLEM. TRY . LCL_PROBLEM=>TEST_NO_CHECK( ). CATCH ZCX_NO_CHECK. WRITE: / 'No check exception encountered'. ENDTRY. TRY . LCL_PROBLEM=>TEST_DYNAMIC_CHECK( ). CATCH ZCX_DYNAMIC_CHECK. WRITE: / 'Dynamic check exception encountered'. ENDTRY. TRY . LCL_PROBLEM=>TEST_STATIC_CHECK( ).


58 | P a g e CATCH ZCX_STATIC_CHECK. WRITE: / 'Static check exception encountered'. ENDTRY. ENDMETHOD. ENDCLASS. START-OF-SELECTION. WRITE: / 'START'. LCL_CALL_PROBLEM=>CALL_PROBLEM( ). WRITE: / 'END'.

*&---------------------------------------------------------------------* *& Report Z00_OBJ20_CREATE_PERSISTENT *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

Z00_OBJ20_CREATE_PERSISTENT.

PARAMETERS: PA_REGNO PA_COLOR PA_SPEED PA_NUMPS DATA: GO_AGENT GO_CAR

TYPE TYPE TYPE TYPE

ZREGNO DEFAULT 'DDD4444', ZCOLOR DEFAULT 'PURPLE', ZSPEED DEFAULT 50, ZNUMPASS DEFAULT 3.

TYPE REF TO ZCA_PCAR00, TYPE REF TO ZCL_PCAR00.

START-OF-SELECTION. GO_AGENT = ZCA_PCAR00=>AGENT. TRY. GO_CAR = GO_AGENT->CREATE_PERSISTENT( I_COLOR = PA_COLOR I_NUMPASS = PA_NUMPS I_REGNO = PA_REGNO I_SPEED = PA_SPEED ). GO_CAR->DISPLAY_ATTRIBUTES( ). COMMIT WORK. CATCH CX_OS_OBJECT_EXISTING . WRITE: / 'Object existing'. ENDTRY.

*&---------------------------------------------------------------------* *& Report Z00_OBJ20_ACCELERATE_CAR *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------*


59 | P a g e REPORT

Z00_OBJ20_ACCELERATE_CAR.

PARAMETERS: PA_REGNO TYPE ZREGNO DEFAULT 'DDD4444', PA_DSPED TYPE ZSPEED DEFAULT '1'. DATA: GO_AGENT GO_CAR

TYPE REF TO ZCA_PCAR00, TYPE REF TO ZCL_PCAR00.

START-OF-SELECTION. GO_AGENT = ZCA_PCAR00=>AGENT. TRY. GO_CAR = GO_AGENT->GET_PERSISTENT( I_REGNO = PA_REGNO GO_CAR->DISPLAY_ATTRIBUTES( ). WRITE: / 'Accelerate car by', PA_DSPED. GO_CAR->ACCELERATE( PA_DSPED ). GO_CAR->DISPLAY_ATTRIBUTES( ). COMMIT WORK. CATCH CX_OS_OBJECT_NOT_FOUND . WRITE: / 'Object not found'. ENDTRY.

).

*&---------------------------------------------------------------------* *& Report Z00_OBJ22_WRITE_TO_SHARED *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

Z00_OBJ22_WRITE_TO_SHARED.

DATA: GO_AREA GO_CATALOG

TYPE REF TO ZCL_AREA00, TYPE REF TO ZCL_CATALOG00.

START-OF-SELECTION. GO_AREA = ZCL_AREA00=>ATTACH_FOR_WRITE( ). CREATE OBJECT GO_CATALOG AREA HANDLE GO_AREA. SELECT * FROM SFLIGHT INTO TABLE GO_CATALOG->IT_SFLIGHT. GO_AREA->SET_ROOT( GO_CATALOG ). GO_AREA->DETACH_COMMIT( ). WRITE: / 'SFLIGHT records written to shared memory'.

*&---------------------------------------------------------------------* *& Report Z00_OBJ23_READ_FROM_SHARED *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------*


60 | P a g e REPORT

Z00_OBJ23_READ_FROM_SHARED.

DATA: GO_AREA TYPE REF TO ZCL_AREA00, GO_CATALOG TYPE REF TO ZCL_CATALOG00. DATA: GS_SFLIGHT TYPE SFLIGHT. START-OF-SELECTION. GO_AREA = ZCL_AREA00=>ATTACH_FOR_READ( ). GO_CATALOG = GO_AREA->ROOT. LOOP AT GO_CATALOG->IT_SFLIGHT INTO GS_SFLIGHT. WRITE: / GS_SFLIGHT-CARRID, GS_SFLIGHT-CONNID, GS_SFLIGHT-FLDATE, GS_SFLIGHT-PLANETYPE, GS_SFLIGHT-SEATSMAX, GS_SFLIGHT-SEATSOCC. ENDLOOP. GO_AREA->DETACH( ).

*&---------------------------------------------------------------------* *& Report Z00_OBJ24_STATIC_PROGRAMMING *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

Z00_OBJ24_STATIC_PROGRAMMING.

DATA: GT_SFLIGHT TYPE TABLE OF SFLIGHT, GS_SFLIGHT TYPE SFLIGHT. START-OF-SELECTION. SELECT * FROM SFLIGHT INTO TABLE GT_SFLIGHT. LOOP AT GT_SFLIGHT INTO GS_SFLIGHT. WRITE: / GS_SFLIGHT-CARRID, GS_SFLIGHT-CONNID, GS_SFLIGHT-FLDATE, GS_SFLIGHT-PLANETYPE, GS_SFLIGHT-SEATSMAX, GS_SFLIGHT-SEATSOCC. ENDLOOP. *&---------------------------------------------------------------------* *& Report Z00_OBJ25_FIELD_SYMBOLS1 *&


61 | P a g e *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

Z00_OBJ25_FIELD_SYMBOLS1.

DATA: GV_INT TYPE I VALUE 15. FIELD-SYMBOLS: <FS_INT> TYPE I. START-OF-SELECTION. *

WRITE: / <FS_INT>. ASSIGN GV_INT TO <FS_INT>. WRITE: / GV_INT, <FS_INT>.

*

ADD 2 TO <FS_INT>. <FS_INT> = 111. WRITE: / GV_INT, <FS_INT>. UNASSIGN <FS_INT>. IF <FS_INT> IS ASSIGNED. WRITE: / GV_INT, <FS_INT>. ELSE. WRITE: / '<FS_INT> IS NOT ASSIGNED'. ENDIF.

*&---------------------------------------------------------------------* *& Report Z00_OBJ25_FIELD_SYMBOLS1 *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT DATA: DATA: DATA: DATA:

Z00_OBJ26_GENERIC_FIELD_SYMBOL. GV_INT TYPE I VALUE 15. GV_CHAR TYPE C LENGTH 11 VALUE 'Hello World'. GS_SFLIGHT TYPE SFLIGHT. GT_SFLIGHT TYPE TABLE OF SFLIGHT.

FIELD-SYMBOLS: <FS_INT> TYPE I. FIELD-SYMBOLS: <FS_SFLIGHT> TYPE SFLIGHT. FIELD-SYMBOLS: <FS_ANY> TYPE ANY, <FS_COMP> TYPE ANY, <FS_ANY_TABLE> TYPE ANY TABLE. START-OF-SELECTION. *

WRITE: / <FS_INT>. WRITE: / 'Generic field symbols pointing to elementary types.'. ASSIGN GV_INT TO <FS_INT>.


62 | P a g e ASSIGN GV_INT TO <FS_ANY>. WRITE: / GV_INT, <FS_INT>, <FS_ANY>. ADD 2 TO <FS_INT>. ADD 2 TO <FS_ANY>. WRITE: / GV_INT, <FS_INT>, <FS_ANY>. ULINE. ASSIGN GV_CHAR TO <FS_ANY>. WRITE: / GV_CHAR, <FS_ANY>. ULINE. WRITE: SELECT ASSIGN ASSIGN

/ 'Generic field symbols pointing to structures.'. SINGLE * FROM SFLIGHT INTO GS_SFLIGHT. GS_SFLIGHT TO <FS_SFLIGHT>. GS_SFLIGHT TO <FS_ANY>.

WRITE: / GS_SFLIGHT-CARRID, GS_SFLIGHT-CONNID, GS_SFLIGHT-FLDATE, GS_SFLIGHT-PLANETYPE, GS_SFLIGHT-SEATSMAX, GS_SFLIGHT-SEATSOCC. WRITE: / <FS_SFLIGHT>-CARRID, <FS_SFLIGHT>-CONNID, <FS_SFLIGHT>-FLDATE, <FS_SFLIGHT>-PLANETYPE, <FS_SFLIGHT>-SEATSMAX, <FS_SFLIGHT>-SEATSOCC. * WRITE: / <FS_ANY>-CARRID. NEW-LINE. ASSIGN COMPONENT 1 OF STRUCTURE <FS_ANY> TO <FS_COMP>. WRITE: <FS_COMP>. ASSIGN COMPONENT 'CARRID' OF STRUCTURE <FS_ANY> TO <FS_COMP>. WRITE: <FS_COMP>. ASSIGN COMPONENT 'CONNID' OF STRUCTURE <FS_ANY> TO <FS_COMP>. WRITE: <FS_COMP>. ASSIGN COMPONENT 4 OF STRUCTURE <FS_ANY> TO <FS_COMP>. WRITE: <FS_COMP>. NEW-LINE. DO. ASSIGN COMPONENT SY-INDEX OF STRUCTURE <FS_ANY> TO <FS_COMP>. IF SY-SUBRC NE 0. EXIT. ENDIF. WRITE: <FS_COMP>. ENDDO. ULINE. WRITE: / 'Generic field symbols pointing to internal tables.'. SELECT * FROM SFLIGHT INTO TABLE GT_SFLIGHT. ASSIGN GT_SFLIGHT TO <FS_ANY_TABLE>. LOOP AT <FS_ANY_TABLE> INTO <FS_ANY>.


63 | P a g e NEW-LINE. DO. ASSIGN COMPONENT SY-INDEX OF STRUCTURE <FS_ANY> TO <FS_COMP>. IF SY-SUBRC NE 0. EXIT. ENDIF. WRITE: <FS_COMP>. ENDDO. ENDLOOP.

*&---------------------------------------------------------------------* *& Report Z00_OBJ24_STATIC_PROGRAMMING *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

Z00_OBJ27_LOOP_ASSIGNING_FS.

DATA: GT_SFLIGHT TYPE TABLE OF SFLIGHT, GS_SFLIGHT TYPE SFLIGHT. FIELD-SYMBOLS: <FS_SFLIGHT> TYPE SFLIGHT. START-OF-SELECTION. SELECT * FROM SFLIGHT INTO TABLE GT_SFLIGHT. * * * *

LOOP AT GT_SFLIGHT INTO GS_SFLIGHT. GS_SFLIGHT-SEATSOCC = 0. MODIFY GT_SFLIGHT FROM GS_SFLIGHT. ENDLOOP. LOOP AT GT_SFLIGHT ASSIGNING <FS_SFLIGHT>. <FS_SFLIGHT>-SEATSOCC = 0. ENDLOOP.

LOOP AT GT_SFLIGHT ASSIGNING <FS_SFLIGHT>. WRITE: / <FS_SFLIGHT>-CARRID, <FS_SFLIGHT>-CONNID, <FS_SFLIGHT>-FLDATE, <FS_SFLIGHT>-PLANETYPE, <FS_SFLIGHT>-SEATSMAX, <FS_SFLIGHT>-SEATSOCC. ENDLOOP.

*&---------------------------------------------------------------------* *& Report Z00_OBJ28_DYNAMIC_SUBSTITUTION *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------*


64 | P a g e REPORT

Z00_OBJ28_DYNAMIC_SUBSTITUTION.

PARAMETERS: PA_TAB

TYPE C LENGTH 30 DEFAULT 'SFLIGHT'.

DATA: GV_FIELD_NAME TYPE C LENGTH 30 VALUE 'GT_SFLIGHT'. DATA: GT_SFLIGHT TYPE TABLE OF SFLIGHT. FIELD-SYMBOLS: <FS_ANY_TABLE> TYPE ANY TABLE. START-OF-SELECTION. WRITE: / 'SFLIGHT', 'Table'. WRITE: / PA_TAB, 'Table'. SELECT * FROM SFLIGHT INTO TABLE GT_SFLIGHT. SELECT * FROM (PA_TAB) INTO TABLE GT_SFLIGHT. ASSIGN GT_SFLIGHT TO <FS_ANY_TABLE>. ASSIGN (GV_FIELD_NAME) TO <FS_ANY_TABLE>. BREAK-POINT.

*&---------------------------------------------------------------------* *& Report Z00_OBJ25_FIELD_SYMBOLS1 *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

Z00_OBJ29_DATA_REFERENCES.

DATA: GV_INT TYPE I VALUE 15. FIELD-SYMBOLS: <FS_INT> TYPE I. DATA: GR_INT TYPE REF TO I.

START-OF-SELECTION. ASSIGN GV_INT TO <FS_INT>. GET REFERENCE OF GV_INT INTO GR_INT. WRITE: / GV_INT, <FS_INT>, GR_INT->*. ADD 2 TO <FS_INT>. ADD 2 TO GR_INT->*. WRITE: / GV_INT, <FS_INT>, GR_INT->*. UNASSIGN <FS_INT>. CLEAR GR_INT. IF <FS_INT> IS ASSIGNED. WRITE: / GV_INT, <FS_INT>.


65 | P a g e ELSE. WRITE: / '<FS_INT> IS NOT ASSIGNED'. ENDIF. IF GR_INT IS BOUND. WRITE: / GV_INT, <FS_INT>, GR_INT->*. ELSE. WRITE: / 'GR_INT IS NOT BOUND'. ENDIF.

*&---------------------------------------------------------------------* *& Report Z00_OBJ25_FIELD_SYMBOLS1 *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT DATA: DATA: DATA: DATA:

Z00_OBJ30_GENERIC_DATA_REF. GV_INT TYPE I VALUE 15. GV_CHAR TYPE C LENGTH 11 VALUE 'Hello World'. GS_SFLIGHT TYPE SFLIGHT. GT_SFLIGHT TYPE TABLE OF SFLIGHT.

DATA: GR_DATA GR_INT

TYPE REF TO DATA, TYPE REF TO I.

FIELD-SYMBOLS: <FS_ANY> TYPE ANY, <FS_COMP> TYPE ANY, <FS_ANY_TABLE> TYPE ANY TABLE. START-OF-SELECTION. WRITE: / 'Generic data reference pointing to elementary types.'. GET REFERENCE OF GV_INT INTO GR_INT. GET REFERENCE OF GV_INT INTO GR_DATA. ASSIGN GR_DATA->* TO <FS_ANY>. WRITE: / GV_INT, GR_INT->*, <FS_ANY>. ULINE. WRITE: / 'Generic data reference pointing to structures.'. SELECT SINGLE * FROM SFLIGHT INTO GS_SFLIGHT. GET REFERENCE OF GS_SFLIGHT INTO GR_DATA. ASSIGN GR_DATA->* TO <FS_ANY>. NEW-LINE. DO. ASSIGN COMPONENT SY-INDEX OF STRUCTURE <FS_ANY> TO <FS_COMP>. IF SY-SUBRC NE 0. EXIT. ENDIF. WRITE: <FS_COMP>. ENDDO. ULINE. WRITE: / 'Generic data reference pointing to internal tables.'.


66 | P a g e SELECT * FROM SFLIGHT INTO TABLE GT_SFLIGHT. GET REFERENCE OF GT_SFLIGHT INTO GR_DATA. ASSIGN GR_DATA->* TO <FS_ANY_TABLE>. LOOP AT <FS_ANY_TABLE> INTO <FS_ANY>. NEW-LINE. DO. ASSIGN COMPONENT SY-INDEX OF STRUCTURE <FS_ANY> TO <FS_COMP>. IF SY-SUBRC NE 0. EXIT. ENDIF. WRITE: <FS_COMP>. ENDDO. ENDLOOP.

*&---------------------------------------------------------------------* *& Report Z00_OBJ31_CREATE_DATA *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

Z00_OBJ31_CREATE_DATA.

DATA: GV_INT TYPE I VALUE 15. DATA: GR_INT TYPE REF TO I. PARAMETERS: PA_TAB TYPE C LENGTH 30 DEFAULT 'SFLIGHT'. DATA: GS_SFLIGHT TYPE SFLIGHT. DATA: GR_DATA TYPE REF TO DATA. FIELD-SYMBOLS: <FS_ANY> TYPE ANY. START-OF-SELECTION. *

GET REFERENCE OF GV_INT INTO GR_INT. CREATE DATA GR_INT. GR_INT->* = 22. WRITE: / GV_INT, GR_INT->*. ULINE. WRITE: / 'Working with structures'. SELECT SINGLE * FROM SFLIGHT INTO GS_SFLIGHT. CREATE DATA GR_DATA TYPE (PA_TAB). ASSIGN GR_DATA->* TO <FS_ANY>. SELECT SINGLE * FROM (PA_TAB) INTO <FS_ANY>. BREAK-POINT.


67 | P a g e *&---------------------------------------------------------------------* *& Report Z00_OBJ24_STATIC_PROGRAMMING *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

Z00_OBJ32_DYNAMIC_PROGRAMMING.

*DATA: GT_SFLIGHT TYPE TABLE OF SFLIGHT, * GS_SFLIGHT TYPE SFLIGHT. PARAMETERS: PA_TAB TYPE C LENGTH 30 DEFAULT 'SFLIGHT'. DATA: GR_STRUC GR_TABLE

TYPE REF TO DATA, TYPE REF TO DATA.

FIELD-SYMBOLS: <FS_STRUC> TYPE ANY, <FS_TABLE> TYPE ANY TABLE, <FS_COMP> TYPE ANY. START-OF-SELECTION. CREATE DATA GR_STRUC TYPE (PA_TAB). CREATE DATA GR_TABLE TYPE TABLE OF (PA_TAB). ASSIGN GR_STRUC->* TO <FS_STRUC>. ASSIGN GR_TABLE->* TO <FS_TABLE>. SELECT * FROM (PA_TAB) INTO TABLE <FS_TABLE>. LOOP AT <FS_TABLE> INTO <FS_STRUC>. NEW-LINE. DO. ASSIGN COMPONENT SY-INDEX OF STRUCTURE <FS_STRUC> TO <FS_COMP>. IF SY-SUBRC NE 0. EXIT. ENDIF. WRITE: <FS_COMP>. ENDDO. ENDLOOP.

*&---------------------------------------------------------------------* *& Report Z00_OBJ33_RTTI *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

Z00_OBJ33_RTTI.

* Runtime Type Identification DATA: GV_CARRID

TYPE S_CARR_ID,


68 | P a g e GS_SFLIGHT GT_SFLIGHT

TYPE SFLIGHT, TYPE SORTED TABLE OF SFLIGHT WITH UNIQUE KEY CARRID CONNID FLDATE. DATA: GO_CAR TYPE REF TO ZCL_CAR00, GO_OCEANITEM TYPE REF TO ZIF_OCEANITEM00, GR_INT TYPE REF TO I. START-OF-SELECTION. WRITE: / 'Passing in GV_CARRID'. PERFORM DESCRIBE_ME USING GV_CARRID. SKIP. WRITE: / 'Passing in GS_SFLIGHT'. PERFORM DESCRIBE_ME USING GS_SFLIGHT. SKIP. WRITE: / 'Passing in GT_SFLIGHT'. PERFORM DESCRIBE_ME USING GT_SFLIGHT. SKIP. WRITE: / 'Passing in GO_CAR'. PERFORM DESCRIBE_ME USING GO_CAR. SKIP. WRITE: / 'Passing in GO_OCEANITEM'. PERFORM DESCRIBE_ME USING GO_OCEANITEM. SKIP. WRITE: / 'Passing in GR_INT'. PERFORM DESCRIBE_ME USING GR_INT.

FORM DESCRIBE_ME USING F_PARAM TYPE ANY. DATA: LO_TYPEDESCR TYPE REF TO CL_ABAP_TYPEDESCR, LO_ELEMDESCR TYPE REF TO CL_ABAP_ELEMDESCR, LO_STRUCTDESCR TYPE REF TO CL_ABAP_STRUCTDESCR, LO_TABLEDESCR TYPE REF TO CL_ABAP_TABLEDESCR. DATA: LS_COMPONENTS TYPE abap_compdescr. LO_TYPEDESCR = CL_ABAP_TYPEDESCR=>DESCRIBE_BY_DATA( P_DATA = F_PARAM WRITE: / 'ABSOLUTE_NAME = ', LO_TYPEDESCR->ABSOLUTE_NAME. WRITE: / 'TYPE_KIND = ', LO_TYPEDESCR->TYPE_KIND. WRITE: / 'LENGTH = ', LO_TYPEDESCR->LENGTH. WRITE: / 'DECIMALS = ', LO_TYPEDESCR->DECIMALS. CASE LO_TYPEDESCR->KIND. WHEN CL_ABAP_TYPEDESCR=>KIND_ELEM. WRITE: / 'It''s an elementary type.'. LO_ELEMDESCR ?= LO_TYPEDESCR. WRITE: / 'EDIT_MASK = ', LO_ELEMDESCR->EDIT_MASK. WRITE: / 'HELP_ID = ', LO_ELEMDESCR->HELP_ID. WRITE: / 'OUTPUT_LENGTH = ', LO_ELEMDESCR->OUTPUT_LENGTH WHEN CL_ABAP_TYPEDESCR=>KIND_STRUCT. WRITE: / 'It''s a structure type.'. LO_STRUCTDESCR ?= LO_TYPEDESCR. WRITE: / 'STRUCT_KIND = ', LO_STRUCTDESCR->STRUCT_KIND.

.

).


69 | P a g e WRITE: / 'HAS_INCLUDE = ', LO_STRUCTDESCR->HAS_INCLUDE. LOOP AT LO_STRUCTDESCR->COMPONENTS INTO LS_COMPONENTS. WRITE: / LS_COMPONENTS-length, LS_COMPONENTS-decimals, LS_COMPONENTS-type_kind, LS_COMPONENTS-name. ENDLOOP. WHEN CL_ABAP_TYPEDESCR=>KIND_TABLE. WRITE: / 'It''s a table type.'. LO_TABLEDESCR ?= LO_TYPEDESCR. WHEN CL_ABAP_TYPEDESCR=>KIND_REF. WRITE: / 'It''s a reference type.'. WHEN CL_ABAP_TYPEDESCR=>KIND_CLASS. WRITE: / 'It''s a class type.'. WHEN CL_ABAP_TYPEDESCR=>KIND_INTF. WRITE: / 'It''s an interface type.'. ENDCASE. ENDFORM.

*&---------------------------------------------------------------------* *& Report Z00_OBJ34_DESCRIBE_CLASS *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

Z00_OBJ34_DESCRIBE_CLASS.

DATA: GO_CAR TYPE REF TO ZCL_CAR00. DATA: GO_CLASSDESCR TYPE REF TO CL_ABAP_CLASSDESCR. DATA: GS_METHODS TYPE abap_methdescr. DATA: GS_PARAMS TYPE abap_parmdescr. START-OF-SELECTION. GO_CLASSDESCR ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_NAME( 'ZCL_CAR00' ). WRITE: WRITE: WRITE: WRITE: WRITE:

* *

/ / / / /

'ABSOLUTE_NAME 'TYPE_KIND 'LENGTH 'DECIMALS 'KIND

= = = = =

', ', ', ', ',

GO_CLASSDESCR->ABSOLUTE_NAME . GO_CLASSDESCR->TYPE_KIND. GO_CLASSDESCR->LENGTH. GO_CLASSDESCR->DECIMALS. GO_CLASSDESCR->KIND.

LOOP AT GO_CLASSDESCR->METHODS INTO GS_METHODS. WRITE: / 'parameters ', GS_METHODS-parameters . WRITE: / 'exceptions ', GS_METHODS-exceptions . WRITE: / 'Method name', GS_METHODS-name . LOOP AT GS_METHODS-PARAMETERS INTO GS_PARAMS. WRITE: / 'length ', GS_PARAMS-length . WRITE: / 'decimals ', GS_PARAMS-decimals . WRITE: / 'type_kind ', GS_PARAMS-type_kind . WRITE: / 'name ', GS_PARAMS-name . WRITE: / 'parm_kind ', GS_PARAMS-parm_kind .


70 | P a g e WRITE: / 'by_value ', GS_PARAMS-by_value . WRITE: / 'is_optional', GS_PARAMS-is_optional. ENDLOOP. ENDLOOP.


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.