这一节通过上节给出的例子,定义类的初始化。
*----------------------------------------------------------------------*
* INCLUDE Z_SAMPLE_LCL_AIRPLANE *
*----------------------------------------------------------------------*
******************************************
* Definition part
******************************************
CLASS lcl_airplane DEFINITION.
*------------------------------
* Public section
*------------------------------
PUBLIC SECTION.
TYPES: t_name(25) TYPE c.
METHODS:
constructor importing p2_name type t_name
p2_planetype TYPE saplane-planetype,
set_attributes IMPORTING p_name TYPE t_name
p_planetype TYPE saplane-planetype,
display_attributes,
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 = p2_name.
planetype = p2_planetype.
ENDMETHOD.
METHOD set_attributes.
name = p_name.
planetype = p_planetype.
ENDMETHOD.
METHOD display_attributes.
WRITE:/ 'Name:', name, 'Planetype:', planetype.
ENDMETHOD.
METHOD display_n_o_airplanes.
WRITE: / 'No. planes:', n_o_airplanes.
ENDMETHOD.
ENDCLASS.
---------------------------------------------------------------------------------------------
REPORT ZBOBO_OO_011 .
Include z_sample_lcl_airplane.
* Create reference to class lcl_airplane
DATA: airplane1 TYPE REF TO lcl_airplane,
airplane2 TYPE REF TO lcl_airplane.
START-OF-SELECTION.
* Create instance
CREATE OBJECT airplane1
Exporting p2_name = ‘Airp1’
P2_planetype = ‘BoBo’.
CALL METHOD: airplane1->display_n_o_airplanes.
CREATE OBJECT airplane2
Exporting p2_name = ‘Airp1’
P2_planetype = ‘BoBo’.
* Setting attributes using a method with parameters
* 修改了plane1的属性,但是plane2的没有改变
CALL METHOD airplane1->set_attributes EXPORTING p_name = 'Kurt'
p_planetype = 'MD80'.
END-OF-SELECTION.
* Using methods
* 显示已经修改过的属性
CALL METHOD: airplane1->display_n_o_airplanes,
airplane1->display_attributes.
* 初始化的属性
CALL METHOD: airplane2->display_n_o_airplanes,
Airplane2->display_attributes.
网友评论