引用:翱翔云天
这一节我们来看一下abap oo的定义语句,包括类,方法,事件,属性,接口。
1. 定义类
******************************************
* Definition part
******************************************
CLASS xxx DEFINITION.
*------------------------------
* Public section
*------------------------------
PUBLIC SECTION.
TYPES:
DATA:
* Static data
CLASS-DATA:
* Methods
METHODS:
* Using the constructor to initialize parameters
constructor IMPORTING xxx type yyy,
* Method with parameters
mm1 IMPORTING iii TYPE ttt.
* Method without parameters
mm2.
* Static methods
CLASS-METHODS:
*---------------------------------------------------*
* Protected section. Also accessible by subclasses
*---------------------------------------------------
PROTECTED SECTION.
*---------------------------------------------------
* Private section. Not accessible by subclasses
*---------------------------------------------------
PRIVATE SECTION.
ENDCLASS.
******************************************
* Implementation part
******************************************
CLASS lcl_airplane IMPLEMENTATION.
METHOD constructor.
ENDMETHOD.
METHOD mm1.
ENDMETHOD.
METHOD mm2.
ENDMETHOD.
ENDCLASS.
2. 定义继承
CLASS xxx DEFINITION INHERITING FROM yyy.
3. 定义接口
INTERFACE lif_document
DATA: author type ref to lcl_author.
METHODS: print,
display.
ENDINTERFACE.
CLASS lcl_text_document DEFINITION.
PUBLIC SECTION.
INTERFACES lif_document.
METHODS display.
ENDCLASS.
CLASS lcl_text_document IMPLEMENTTION.
METHOD lif_document~print.
ENDMETHOD.
METHOD lif_document~display
ENDMETHOD.
METHOD display.
ENDMETHOD.
ENDCLASS.
REPORT zzz.
DATA: text_doc TYPE REF TO lcl_document.
Start-of-selection.
CREATE OBJECT text_doc.
CALL METHOD text_doc->lif_document~print.
CALL METHOD text_doc->lif_document~display.
CALL METHOD text_doc->display.
4. 事件
CLASS <classname> DEFINITION.
EVENTS: <event> EXPORTING VALUE (<ex_par>) TYPE type.
CLASS <classname> IMPLEMENTATION.
METHOD <m>:
RAISE EVENT <event> EXPORTING <ex_par> = <act_par>.
Handling events:
CLASS <classname> DEFINITION.
METHODS: <on_event> FOR <event> OF {<classname> ! <interface> }
IMPORTING <imp_par1>...<imp_parN> SENDER.
Setting handler
SET HANDLER <ref_handle> <on_event> FOR {<ref_sender> ! FOR ALL INSTANCES}
[ACTIVATION <var>]