2008年(49)
分类:
2008-04-09 17:51:00
需求背景: 我写include 文件,让别人调用. include文件中有一个 server_download_data 子程序, 在这个子程序中,需要调用另一个子程序concatenate_error_record.
FORM server_download_data.
IF need_err_file = 'X'.
OPEN DATASET pa_efile FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
LOOP AT err_msg_tab.
PERFORM concatenate_error_record.
TRANSFER g_error_record TO pa_efile.
ENDLOOP.
CLOSE DATASET pa_efile.
ENDIF.
ENDFORM.
但是这里的 concatenate_error_record ,每个用户的具体定义都不同.但是都只是在server_download_data中调用. 所以我需要在虚基类中定义一个一般的方法server_download_data, 然后把concatenate_error_record定义为虚函数. 在server_download_data中通过函数名字调用concatenate_error_record. 然后用户在派生类中根据自己的需要实现虚函数.
以下程序在虚基类中定义一个普通方法 print, 让用户自己在派生类中定义方法add的具体实现.
REPORT Z_TEMP_DEMO4.
CLASS base_class DEFINITION ABSTRACT.
PUBLIC SECTION.
DATA: num1 TYPE i VALUE 1,
num2 TYPE i VALUE 100,
num3 TYPE i VALUE 1000,
sum TYPE i.
METHODS add ABSTRACT
EXPORTING p_sum TYPE i.
METHODS print.
ENDCLASS.
CLASS base_class IMPLEMENTATION.
METHOD print.
CALL METHOD add IMPORTING p_sum = sum.
write: / 'Sum is', sum.
ENDMETHOD.
ENDCLASS.
CLASS derived_class DEFINITION INHERITING FROM base_class.
PUBLIC SECTION.
METHODS add REDEFINITION.
ENDCLASS.
CLASS derived_class IMPLEMENTATION.
METHOD add.
p_sum = num1 + num2.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA my_object TYPE REF TO derived_class.
CREATE OBJECT my_object.
CALL METHOD my_object->print.