Open nangnangchuai opened 5 years ago
其中定义时候的importing代表传入的参数,当调用方法的时候用exporting去接参数
公开方法传参数
class c1 DEFINITION. PUBLIC SECTION. DATA:I_char type char20. METHODS:set_data IMPORTING p_imp TYPE char20, "传入参数 get_data EXPORTING p_exp TYPE char20. "传出参数
ENDCLASS.
CLASS c1 IMPLEMENTATION. METHOD set_data. I_char = p_imp. ENDMETHOD.
METHOD get_data.
p_exp = I_char.
ENDMETHOD.
ENDCLASS.
DATA:go_cref TYPE REF TO c1. DATA:gv_data TYPE char20.
START-OF-SELECTION. CREATE OBJECT go_cref. call METHOD : go_cref->set_data EXPORTING p_imp = 'test class method'. "I_CHAR=test class method
call METHOD: go_cref->get_data IMPORTING p_exp = gv_data. "将gv_data作为p_exp传进去,使得I_char的值赋值给了gv_data write gv_data. "输出gv_data的结果为test class method
CLASS c1 DEFINITION.
PROTECTED SECTION. "只有继承的类才可以访问
DATA:i_num TYPE i.
ENDCLASS.
CLASS c2 DEFINITION INHERITING FROM c1. "类继承
PUBLIC SECTION.
METHODS: set_data IMPORTING p_imp TYPE i,
get_data EXPORTING p_exp TYPE i.
ENDCLASS. "c2 DEFINITION
CLASS c2 IMPLEMENTATION.
METHOD set_data.
i_num = p_imp.
ENDMETHOD. "set_data
METHOD get_data.
p_exp = i_num + 1.
ENDMETHOD. "get_data
ENDCLASS. "c2 IMPLEMENTATION
DATA:go_cref TYPE REF TO c2.
DATA:gv_data TYPE i.
START-OF-SELECTION.
CREATE OBJECT go_cref. "创建对象
CALL METHOD : go_cref->set_data
EXPORTING p_imp = '10'. "这里exporting代表传入参数
CALL METHOD: go_cref->get_data
IMPORTING p_exp = gv_data. "将gv_data作为p_exp传进去,importing代表传出的参 数
WRITE gv_data. "输出gv_data的结果为11
class cls DEFINITION INHERITING FROM OBJECT.
PUBLIC SECTION.
METHODs:create.
ENDCLASS.
class cls IMPLEMENTATION.
METHOD create.
write : / '对象被创建了'.
ENDMETHOD.
ENDCLASS.
DATA:go_obj1 type REF TO cls.
DATA:go_obj2 type REF TO cls.
START-OF-SELECTION.
CREATE OBJECT go_obj1.
go_obj2 = go_obj1. "使用=创建了对象
CALL METHOD: go_obj2->create.
class vehicle DEFINITION INHERITING FROM object.
PUBLIC SECTION.
METHODs:create, "公开的方法
drive.
PROTECTED SECTION. "私有的变量
DATA speed TYPE i VALUE '100'.
ENDCLASS.
CLASS plane DEFINITION INHERITING FROM vehicle.
PUBLIC SECTION. "飞机有飞的方法继承vehicle类
METHODs:fly.
ENDCLASS.
class vehicle IMPLEMENTATION.
METHOD create.
ENDMETHOD.
METHOD drive.
speed = speed + 100.
WRITE : /'当前的速度为', speed.
ENDMETHOD.
ENDCLASS.
CLASS plane IMPLEMENTATION.
METHOD fly.
speed = speed + 1000. "f飞机类自己的方法的实现
ENDMETHOD.
ENDCLASS.
data: car TYPE REF TO vehicle,
air TYPE REF TO vehicle.
START-OF-SELECTION.
CREATE OBJECT car.
air ?= car.
CALL METHOD air->drive. "输出200
CLASS c5 DEFINITION.
PUBLIC SECTION.
DATA: gt_itab TYPE TABLE OF sflight,
gs_str TYPE sflight.
METHODS: get_data,
wri_data.
ENDCLASS. "c5 DEFINITION
CLASS c5 IMPLEMENTATION.
METHOD: get_data.
SELECT * INTO TABLE gt_itab
FROM sflight
UP TO 5 ROWS.
ENDMETHOD.
METHOD: wri_data.
LOOP AT gt_itab INTO gs_str.
WRITE:/ gs_str-carrid,gs_str-connid.
ENDLOOP.
ENDMETHOD. "get_data
ENDCLASS. ".
DATA: go_oref TYPE REF TO c5.
DATA: gv_mth TYPE string.
START-OF-SELECTION.
CREATE OBJECT go_oref.
gv_mth = 'GET_DATA'.
call METHOD go_oref->(gv_mth). "动态调用方法名称为get_data的方法
gv_mth = 'WRI_DATA'.
call METHOD go_oref->(gv_mth). "动态调用方法名册名称为wri_data的方法
CLASS c5 IMPLEMENTATION.
METHOD: get_data.
SELECT * INTO TABLE gt_itab
FROM sflight
UP TO 5 ROWS.
ENDMETHOD. "get_data
ENDCLASS. ".
DATA: go_oref TYPE REF TO c5.
DATA: gv_mth TYPE string.
DATA:gv_attr TYPE string.
FIELD-SYMBOLS: <fs1> TYPE STANDARD TABLE .
FIELD-SYMBOLS: <fs2> TYPE any .
FIELD-SYMBOLS: <fs3> TYPE any .
START-OF-SELECTION.
CREATE OBJECT go_oref.
gv_mth = 'GET_DATA'.
CALL METHOD go_oref->(gv_mth).
gv_attr = 'gt_itab'.
ASSIGN go_oref->(gv_attr) TO <fs1>.
LOOP AT <fs1> ASSIGNING <fs2>.
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE <fs2> TO <fs3>.
WRITE: <fs3>.
IF sy-subrc NE 0.
EXIT.
ENDIF.
ENDDO.
WRITE: /.
ENDLOOP.
CLASS c1 DEFINITION.
PUBLIC SECTION.
DATA:gt_itab TYPE TABLE OF sflight,
gs_str TYPE sflight.
DATA:gv_carrid TYPE s_carr_id,
gv_connid TYPE s_conn_id.
METHODS:constructor IMPORTING i_carrid TYPE s_carr_id "构造方法的定义
i_connid TYPE s_conn_id.
METHODS:get_data. "定义取数的方法
ENDCLASS. "c1 DEFINITION
*----------------------------------------------------------------------*
* CLASS c1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c1 IMPLEMENTATION.
METHOD:constructor. "构造方法的实现
gv_carrid = i_carrid.
gv_connid = i_connid.
ENDMETHOD. "CONSTRUCTOR
METHOD get_data. "实现取数的方法
SELECT * INTO TABLE gt_itab
FROM sflight
WHERE carrid = gv_carrid
AND connid = gv_connid.
ENDMETHOD. "get_data
ENDCLASS. "c1 IMPLEMENTATION
DATA go_oref TYPE REF TO c1. "声明类的对象
START-OF-SELECTION. "必须写,否则后面的代码无法访问的到
CREATE OBJECT go_oref "创建对象,构造函数需要的值进行传参
EXPORTING
i_carrid = 'AA'
i_connid = '0017'.
CALL METHOD go_oref->get_data. "静态方法调用,调用get_data方法。
LOOP AT go_oref->gt_itab INTO go_oref->gs_str. "遍历对象中表的值到工作区
write: / go_oref->gs_str-carrid,
go_oref->gs_str-connid,
go_oref->gs_str-fldate.
ENDLOOP.
clASS CLS1 DEFINITION INHERITING FROM OBJECT.
PUBLIC SECTION.
METHODS:create.
ENDCLASS.
CLASS CLS1 IMPLEMENTATION.
METHOD create.
WRITE:/ '我是父类的构造方法'.
ENDMETHOD.
ENDCLASS.
CLASS CLS2 DEFINITION INHERITING FROM CLS1.
PUBLIC SECTION.
METHODS:create REDEFINITION. "重定义create方法
ENDCLASS.
CLASS CLS2 IMPLEMENTATION.
METHOD create.
CALL METHOD super->create.
WRITE:'调用父类的create方法'.
ENDMETHOD.
ENDCLASS.
DATA:go_obj TYPE REF TO cls2.
start-of-SELECTION.
create OBJECT :go_obj.
CALL METHOD:go_obj->create.
CLASS c1 DEFINITION.
PUBLIC SECTION.
EVENTS:bowl.
METHODs go_bowl. "声明事件
ENDCLASS. "c1 DEFINITION
*----------------------------------------------------------------------*
* CLASS C2 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c2 DEFINITION.
PUBLIC SECTION.
METHODS letsgo FOR EVENT bowl OF c1. "声明事件处理器的方法为c1类定义的事件做处理
ENDCLASS. "C2 DEFINITION
*----------------------------------------------------------------------*
* CLASS C1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c1 IMPLEMENTATION.
METHOD go_bowl.
WRITE'稍等'.
RAISE EVENT bowl. "触发事件
ENDMETHOD. "go_bowl
ENDCLASS. "C1 IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS C2 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c2 IMPLEMENTATION.
METHOD letsgo.
WRITE'收到马上出发'.
ENDMETHOD.
ENDCLASS. "C2 IMPLEMENTATION
DATA :kim TYPE REF TO c1,
lee TYPE REF TO c2.
START-OF-SELECTION.
CREATE OBJECT :kim,lee.
SET HANDLER lee->letsgo FOR kim. "要使事件处理方法能够对事件进行响应,必须在运行时为相关事件注册该方法,
kim->GO_BOWL( ).
DATA: text TYPE string,
itab TYPE TABLE OF string.
text = 'a abap is a programing language'.
SPLIT text AT space INTO TABLE itab.
LOOP AT itab INTO text.
WRITE :/ text.
ENDLOOP.
定义类声明对象