SAP ABAP

abap object-oriented–使用继承

2018-07-12  本文已影响4人  46b61a5f089d

在这一节中,我们来实现继承。

背景:有两种飞机,一种是客机,一种是货机。显然他们都属于飞机的一种,所以按照对象抽象,得到父类。对于客机而言,有其特殊的属性即座位的数量,而对于货机,同样有特殊的属性能承载的最大重量。所以,原来父类的显示属性的方法不再适用,我们可以把这个方法定义为抽象方法(c++中的虚函数),也可以保留其共同的实现,在子类中重新定义该方法的实现。

Uml图如下:

*----------------------------------------------------------------------*

* INCLUDE Z_SAMPLE_LCL_AIRPLANE *

*----------------------------------------------------------------------*

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

* Definition part

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

CLASS lcl_airplane DEFINITION.

*------------------------------

* Public section

*------------------------------

PUBLIC SECTION.

TYPES: t_name(25) TYPE c.

METHODS:

constructor importing im_name type t_name

im_planetype TYPE saplane-planetype,

display_attributes.

* Static methods

class-methods:

display_n_o_airplanes.

*------------------------------

* Private section

*------------------------------

PRIVATE SECTION.

* Private attributes

DATA: name(25) TYPE c,

planetype TYPE saplane-planetype.

* Private static attribute

CLASS-DATA n_o_airplanes TYPE i.

ENDCLASS.

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

* Implementation part

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

CLASS lcl_airplane IMPLEMENTATION.

METHOD constructor.

* Counts number of instances

n_o_airplanes = n_o_airplanes + 1.

name = im_name.

planetype = im_planetype.

ENDMETHOD.

METHOD display_attributes.

WRITE:/ 'Name:', name, 'Planetype:', planetype.

ENDMETHOD.

METHOD display_n_o_airplanes.

WRITE: / 'No. planes:', n_o_airplanes.

ENDMETHOD.

ENDCLASS.

*----------------------------------------------------------------------*

* INCLUDE Z_SAMPLE_LCL_PA_AIRPLANE *

*----------------------------------------------------------------------*

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

* This is a subclass of class lcl_airplane

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

CLASS lcl_passenger_airplane DEFINITION INHERITING FROM lcl_airplane.

PUBLIC SECTION.

* The constructor contains the parameters from the superclass

* plus the parameters from the subclass

METHODS:

constructor IMPORTING im_name TYPE t_name

im_planetype TYPE saplane-planetype

im_n_o_seats TYPE sflight-seatsmax,

* Redefinition of superclass method display_attributes

display_attributes REDEFINITION.

PRIVATE SECTION.

DATA: n_o_seats TYPE sflight-seatsmax.

ENDCLASS.

CLASS lcl_passenger_airplane IMPLEMENTATION.

METHOD constructor.

* The constructor method of the superclass MUST be called withing the

* construtor

CALL METHOD super->constructor

EXPORTING im_name = im_name

im_planetype = im_planetype.

n_o_seats = im_n_o_seats.

ENDMETHOD.

* The redefined display_attributes method

METHOD display_attributes.

CALL METHOD super->display_attributes.

WRITE: / 'No. seats:', n_o_seats.

ENDMETHOD.

ENDCLASS.

*----------------------------------------------------------------------*

* INCLUDE Z_SAMPLE_LCL_CA_AIRPLANE *

*----------------------------------------------------------------------*

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

* This is a subclass of class lcl_airplane

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

CLASS LCL_CARGO_PLANE DEFINITION INHERITING FROM LCL_AIRPLANE.

PUBLIC SECTION.

METHODS:

* The constructor contains the parameters from the superclass

* plus the parameters from the subclass

CONSTRUCTOR IMPORTING IM_NAME TYPE T_NAME

IM_PLANETYPE TYPE SAPLANE-PLANETYPE

IM_CARGOMAX TYPE SCPLANE-CARGOMAX,

* Redefinition of superclass method display_attributes

DISPLAY_ATTRIBUTES REDEFINITION.

PRIVATE SECTION.

DATA:CARGOMAX TYPE SCPLANE-CARGOMAX.

ENDCLASS.

*---------------------------------------------------------------------*

* CLASS lcl_cargo_plane IMPLEMENTATION

*---------------------------------------------------------------------*

CLASS LCL_CARGO_PLANE IMPLEMENTATION.

METHOD CONSTRUCTOR.

* The constructor method of the superclass MUST be called withing the

* constructor

CALL METHOD SUPER->CONSTRUCTOR

EXPORTING IM_NAME = IM_NAME

IM_PLANETYPE = IM_PLANETYPE.

CARGOMAX = IM_CARGOMAX.

ENDMETHOD.

METHOD DISPLAY_ATTRIBUTES.

* The redefined display_attributes method

CALL METHOD SUPER->DISPLAY_ATTRIBUTES.

WRITE: / 'Cargo max:', CARGOMAX.

ENDMETHOD.

ENDCLASS.

REPORT ZBOBO_OO_011 .

* Super class

Include z_sample_lcl_airplane.

* Sub classes

include z_sample_lcl_pa_airplane.

include z_sample_lcl_ca_airplane.

DATA:

* Type ref to sub classes. Note: It is not necesssary to make typeref to

* the superclass

o_passenger_airplane TYPE REF TO lcl_passenger_airplane,

o_cargo_plane TYPE REF TO lcl_cargo_plane.

START-OF-SELECTION.

* Display initial number of instances = 0

CALL METHOD lcl_airplane=>display_n_o_airplanes.

* Create objects

CREATE OBJECT o_passenger_airplane

EXPORTING

im_name = 'LH505'

im_planetype = 'Boing 747'

im_n_o_seats = 350.

CREATE OBJECT o_cargo_plane

EXPORTING

im_name = 'AR13'

im_planetype = 'DC 3'

im_cargomax = 35.

* Display attributes

CALL METHOD o_passenger_airplane->display_attributes.

CALL METHOD o_cargo_plane->display_attributes.

* Call static method display_n_o_airplanes

* Note: The syntax for calling a superclass method, differs from the

* syntax when calling a subclass method.

* When calling a superclass => must be used instead of ->

CALL METHOD lcl_airplane=>display_n_o_airplanes.
上一篇下一篇

猜你喜欢

热点阅读