REPORT ZCL_VEHICLE1.
INTERFACE if_status.
METHODS write_attributes.
ENDINTERFACE.
CLASS cl_vehicle DEFINITION ABSTRACT.
PUBLIC SECTION.
INTERFACES if_status.
METHODS: speed_up IMPORTING step TYPE i,
stop.
PROTECTED SECTION.
DATA: speed TYPE i,
max_speed TYPE i VALUE 50.
ENDCLASS.
CLASS cl_vehicle IMPLEMENTATION.
METHOD speed_up.
speed = speed + step.
IF speed > max_speed.
speed = max_speed.
ENDIF.
ENDMETHOD.
METHOD stop.
speed = 0.
ENDMETHOD.
METHOD if_status~write_attributes.
WRITE: / 'Speed =', speed,
'Max-Speed =', max_speed.
ENDMETHOD.
ENDCLASS.
CLASS cl_truck DEFINITION INHERITING FROM cl_vehicle.
PUBLIC SECTION.
METHODS: constructor,
if_status~write_attributes REDEFINITION.
ENDCLASS.
CLASS cl_truck IMPLEMENTATION.
METHOD constructor.
super->constructor( ).
max_speed = 100.
ENDMETHOD.
METHOD if_status~write_attributes.
WRITE: / 'Truck'.
super->if_status~write_attributes( ).
ENDMETHOD.
ENDCLASS.
CLASS cl_ship DEFINITION INHERITING FROM cl_vehicle.
PUBLIC SECTION.
DATA name TYPE string READ-ONLY.
METHODS: constructor IMPORTING name TYPE string,
if_status~write_attributes REDEFINITION,
speed_up REDEFINITION.
EVENTS damaged.
ENDCLASS.
CLASS cl_ship IMPLEMENTATION.
METHOD constructor.
super->constructor( ).
max_speed = 30.
me->name = name.
ENDMETHOD.
METHOD if_status~write_attributes.
WRITE: / name.
super->if_status~write_attributes( ).
ENDMETHOD.
METHOD speed_up.
speed = speed + step.
IF speed > max_speed.
max_speed = 0.
stop( ).
RAISE EVENT damaged.
ENDIF.
ENDMETHOD.
ENDCLASS.
CLASS cl_helicopter DEFINITION." CREATE PRIVATE.
PUBLIC SECTION.
CLASS-METHODS: class_constructor,
get_reference RETURNING VALUE(helicopter_ref)
TYPE REF to cl_helicopter.
INTERFACES if_status.
METHODS RECEIVE FOR EVENT DAMAGED OF cl_ship
IMPORTING sender.
PRIVATE SECTION.
CLASS-DATA heli TYPE REF TO cl_helicopter.
DATA caller TYPE string VALUE 'none'.
ENDCLASS.
CLASS cl_helicopter IMPLEMENTATION.
METHOD class_constructor.
CREATE OBJECT heli.
ENDMETHOD.
METHOD get_reference.
helicopter_ref = heli.
ENDMETHOD.
METHOD if_status~write_attributes.
WRITE: / 'Helicopter called by:', caller.
ENDMETHOD.
METHOD RECEIVE.
caller = sender->name.
MESSAGE 'Call to Helicopter' TYPE 'I'.
ENDMETHOD.
ENDCLASS.
* Data declarations
START-OF-SELECTION.
DATA: truck TYPE REF TO cl_vehicle,
ship1 TYPE REF TO cl_ship,
ship2 TYPE REF TO cl_ship,
heli TYPE REF TO cl_helicopter,
heli1 TYPE REF TO cl_helicopter,
status TYPE REF TO if_status,
status_tab LIKE TABLE OF status.
* Implementation
CREATE OBJECT: truck TYPE cl_truck,
ship1 TYPE cl_ship EXPORTING name = 'Morning Glory',
ship2 TYPE cl_ship EXPORTING name = 'Titanic'.
heli = cl_helicopter=>get_reference( ).
heli1 = cl_helicopter=>get_reference( ).
APPEND: truck TO status_tab,
ship1 TO status_tab,
ship2 TO status_tab,
heli TO status_tab,
heli1 to status_tab.
SET HANDLER heli->receive FOR: ship1.
SET HANDLER heli1->receive FOR: ship2.
* SET HANDLER heli->receive FOR ALL INSTANCES. " all objects
truck->speed_up( 60 ).
ship1->speed_up( 40 ).
ship2->speed_up( 40 ).
LOOP AT status_tab INTO status.
status->write_attributes( ).
ULINE.
ENDLOOP.
In this above example.. class vehicle is a super class and truck and ship are sub classes and class helicopter is event handler class.
when i execute this program output is
Truck
Speed = 60 Max-Speed = 100
Morning Glory
Speed = 0 Max-Speed = 0
Titanic
Speed = 0 Max-Speed = 0
Helicopter called by: Titanic
Helicopter called by: Titanic
here Helicopter called by (both are getting as ' titanic' but i need to get one 'morning glory' ship) when both ships cross speed above 30.
and help me regarding parameters , i don't want to give default parametrs like this truck->speed_up( 60 ) , i want to dynamically. plz help me as am new to OOABAP.
thanks