Academia ABAP - AP02 - Aula 02 - OO - 05 - Polimorfismo

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

REPORT zrab2006_11_13.

TYPE-POOLS icon.

INCLUDE ziab2006_11_01.

DATA: r_plane TYPE REF TO lcl_airplane,


r_cargo type ref to lcl_cargo_plane,
r_passenger type ref to lcl_passenger_plane,
r_carrier type ref to lcl_carrier.

START-OF-SELECTION.

create object r_carrier exporting im_name = 'Smile&Fly-Travel'.

CREATE OBJECT r_passenger EXPORTING im_name = 'Lh Berlin' im_planetype = '747-


400' im_seats = 345.
CREATE OBJECT r_cargo EXPORTING im_name = 'AA New York' im_planetype = '737-100'
im_cargo = 533.

r_carrier->add_airplane( r_passenger ).
r_carrier->add_airplane( r_cargo ).

r_carrier->display_attributes( ).

===============================================================================
* ------ ziab2006_11_01

*----- Definicao -----


CLASS lcl_airplane DEFINITION.

*variaveis publicas
PUBLIC SECTION.
CONSTANTS:
pos_1 TYPE i VALUE 30.
METHODS:
constructor
IMPORTING
im_name TYPE string
im_planetype TYPE saplane-planetype,
display_attributes.
CLASS-METHODS:
display_n_o_airplanes,
get_n_o_airplanes RETURNING value(re_count) TYPE i.

*Variaveis privadas
PRIVATE SECTION.
METHODS:
get_technical_attributes
IMPORTING
im_type TYPE saplane-planetype
EXPORTING
ex_weight TYPE s_plan_wei
ex_tankcap TYPE s_capacity.
DATA:
name TYPE string,
planetype TYPE saplane-planetype.
CLASS-DATA:
n_o_airplanes TYPE i.
ENDCLASS. "lcl_airplane DEFINITION

*----- Implementacao -----


CLASS lcl_airplane IMPLEMENTATION.
METHOD constructor.
name = im_name.
planetype = im_planetype.
n_o_airplanes = n_o_airplanes + 1.
ENDMETHOD. "constructor
METHOD display_attributes.
WRITE: / icon_ws_plane AS ICON,
/ 'Name of airplane: ', AT pos_1 name,
/ 'Airplane type: ', AT pos_1 planetype.
ENDMETHOD. "display_attributes
METHOD display_n_o_airplanes.
WRITE: /, / 'Total number of planes: ',
AT pos_1 n_o_airplanes LEFT-JUSTIFIED, /.
ENDMETHOD. "display_n_o_airplanes
METHOD get_n_o_airplanes.
re_count = n_o_airplanes.
ENDMETHOD. "get_n_o_airplanes
METHOD get_technical_attributes.
SELECT SINGLE weight tankcap FROM saplane
INTO (ex_weight, ex_tankcap)
WHERE planetype = im_type.
IF sy-subrc = 0.
ex_weight = 100000.
ex_tankcap = 10000.
ENDIF.
ENDMETHOD. "get_technical_attributes
ENDCLASS. "lcl_airplane IMPLEMENTATION

* ---------------------------
* sub-classe LCL_CARGO_PLANE
* ---------------------------

*----- Definicao -----


CLASS lcl_cargo_plane DEFINITION INHERITING FROM lcl_airplane.

*variaveis publicas
PUBLIC SECTION.
METHODS:
constructor
IMPORTING
im_name TYPE string
im_planetype TYPE saplane-planetype
im_cargo TYPE scplane-cargomax.
METHODS:
display_attributes REDEFINITION.

*variaveis privadas
PRIVATE SECTION.
DATA:
max_cargo TYPE scplane-cargomax.
ENDCLASS. "lcl_cargo_plane DEFINITION

*----- Implementacao -----


CLASS lcl_cargo_plane IMPLEMENTATION.
METHOD constructor.
CALL METHOD super->constructor( im_name = im_name im_planetype =
im_planetype ).
max_cargo = im_cargo.
ENDMETHOD. "constructor

METHOD display_attributes.
super->display_attributes( ).
WRITE: / 'Max cargo: ', max_cargo.
ULINE.
ENDMETHOD. "display_attributes

ENDCLASS. "lcl_cargo_plane IMPLEMENTATION

* -------------------------------
* sub-classe LCL_PASSENGER_PLANE
* -------------------------------

*----- Definicao -----


CLASS lcl_passenger_plane DEFINITION INHERITING FROM lcl_airplane.
*variaveis publicas
PUBLIC SECTION.
METHODS:
constructor
IMPORTING
im_name TYPE string
im_planetype TYPE saplane-planetype
im_seats TYPE sflight-seatsmax.

METHODS:
display_attributes REDEFINITION.
*variaveis privadas
PRIVATE SECTION.
DATA:
max_seats TYPE sflight-seatsmax.
ENDCLASS. "lcl_passenger_plane DEFINITION

*----- Implementacao -----


CLASS lcl_passenger_plane IMPLEMENTATION.
METHOD constructor.
CALL METHOD super->constructor( im_name = im_name im_planetype =
im_planetype ).
max_seats = im_seats.
ENDMETHOD. "constructor

METHOD display_attributes.
super->display_attributes( ).
WRITE: / 'Max seats: ', max_seats.
ULINE.
ENDMETHOD. "display_attributes
ENDCLASS. "lcl_passenger_plane IMPLEMENTATION

* -----------------------
* sub-classe LCL_CARRIER
* -----------------------

*----- Definicao -----


CLASS lcl_carrier DEFINITION.
*variaveis publicas
PUBLIC SECTION.
METHODS:
constructor
IMPORTING
im_name TYPE string,
get_name RETURNING value(ex_name) TYPE string,
add_airplane IMPORTING im_plane TYPE REF TO lcl_airplane,
display_airplanes,
display_attributes.

*variaveis privadas
PRIVATE SECTION.
DATA:
name TYPE string,
airplane_list TYPE TABLE OF REF TO lcl_airplane.
ENDCLASS. "lcl_carrier DEFINITION

*----- Implementacao -----


CLASS lcl_carrier IMPLEMENTATION.
METHOD get_name.
ex_name = name.
ENDMETHOD. "get_name

METHOD constructor.
name = im_name.
ENDMETHOD. "constructor

METHOD add_airplane.
APPEND im_plane TO airplane_list.
ENDMETHOD. "add_airplane

METHOD display_attributes.
WRITE: icon_flight AS ICON, name. ULINE. ULINE.
display_airplanes( ).
ENDMETHOD. "display_attributes

METHOD display_airplanes.
DATA: r_plane TYPE REF TO lcl_airplane.
LOOP AT airplane_list INTO r_plane.
r_plane->display_attributes( ).
ENDLOOP.
ENDMETHOD. "display_airplanes

ENDCLASS. "lcl_carrier IMPLEMENTATION

You might also like