abap-面向对象event事件类的初始用
1 REPORT Y_OOALV_TEST. 2 TYPE-POOLS:SLIS. 3 PARAMETERS:P_COUNT TYPE I. 4 5 CLASS GET_SPFLI DEFINITION. "理解为基础类 6 PUBLIC SECTION. 7 METHODS:GET_DATA IMPORTING AMOUNT TYPE I. "传入参数 8 EVENTS:DATA_EXIST EXPORTING VALUE(DATA) TYPE ANY." 传出参数 参照类型是自己想的 后面找到对的再改 9 PRIVATE SECTION. 10 DATA:IT_SPFLI TYPE STANDARD TABLE OF SPFLI, 11 WA_SPFLI TYPE SPFLI. 12 ENDCLASS. 13 14 CLASS SET_HANDLER DEFINITION."理解为事件类 15 PUBLIC SECTION. 16 METHODS:HANDLER_RAISED FOR EVENT DATA_EXIST OF GET_SPFLI IMPORTING DATA. "理解为为某个类的事件而产生的方法,相当于将2个类关联起来 17 PRIVATE SECTION. 18 ENDCLASS. 19 20 CLASS GET_SPFLI IMPLEMENTATION. "基础类的实现 21 METHOD:GET_DATA. 22 SELECT * FROM SPFLI INTO CORRESPONDING FIELDS OF TABLE IT_SPFLI UP TO AMOUNT ROWS. "根据传入参数取数 23 IF SY-SUBRC = 0. 24 RAISE EVENT DATA_EXIST EXPORTING DATA = IT_SPFLI. "取数成功,则执行事件 25 ELSE. 26 WRITE '取数失败'. 27 ENDIF. 28 ENDMETHOD. 29 ENDCLASS. 30 31 CLASS SET_HANDLER IMPLEMENTATION. 32 METHOD:HANDLER_RAISED."事件方法 33 CL_DEMO_OUTPUT=>DISPLAY( DATA ). 34 ENDMETHOD. 35 ENDCLASS. 36 37 START-OF-SELECTION. 38 DATA:DISPLAY_DATA TYPE REF TO GET_SPFLI. 39 DATA:HANDLE TYPE REF TO SET_HANDLER. 40 CREATE OBJECT:DISPLAY_DATA,HANDLE. 41 SET HANDLER HANDLE->HANDLER_RAISED FOR ALL INSTANCES."事件注册 ,必须要有 42 DISPLAY_DATA->GET_DATA( P_COUNT )."执行方法