*&---------------------------------------------------------------------* *& Report ZTEST20131106_2 *& *&---------------------------------------------------------------------* *& *&关于事件的实例 *&---------------------------------------------------------------------* REPORT ztest20131106_2. *----------------------------------------------------------------------* * CLASS vehicle DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS vehicle DEFINITION INHERITING FROM object. "定义类vehicle,这边是继承基类object. PUBLIC SECTION. EVENTS:too_fast. "定义事件too_fast METHODS:accelerate, show_speed. PROTECTED SECTION. DATA speed TYPE i. ENDCLASS. "vehicle DEFINITION *----------------------------------------------------------------------* * CLASS vehicle IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS vehicle IMPLEMENTATION. "方法的实现 METHOD accelerate. speed = speed + 1. IF speed > 5. RAISE EVENT too_fast. " 在方法accelerate中触发事件too_fast,静态事件需要通过静态方法中触发 speed = 5. ENDIF. ENDMETHOD. "accelerate METHOD show_speed. WRITE: / 'Speed:',speed. ENDMETHOD. "show_speed ENDCLASS. "vehicle IMPLEMENTATION *----------------------------------------------------------------------* * CLASS handler DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS handler DEFINITION. "定义类handler PUBLIC SECTION. METHODS handle_excess "在handler类下定义事件too_fast的处理方法handle_excess ,FOR EVENT too_fast OF vehicle说明事件是在vehicle类下的 FOR EVENT too_fast OF vehicle. ENDCLASS. "handler DEFINITION *----------------------------------------------------------------------* * CLASS handler IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS handler IMPLEMENTATION. "方法的实现 METHOD handle_excess. WRITE: / 'Speed can not be too fast..'. * speed = 10. ENDMETHOD. "handle_excess ENDCLASS. "handler IMPLEMENTATION DATA: o_vehicle TYPE REF TO vehicle, "定义类的引用 o_handle TYPE REF TO handler. START-OF-SELECTION. CREATE OBJECT:o_vehicle,o_handle. "创建类得实例 SET HANDLER o_handle->handle_excess FOR ALL INSTANCES. "注册事件处理方法,这边FOR ALL INSTANCES是为处理器注册所有可以触发该事件的实例,如 果是for o_vehicle表示指定注册到o_vehicle下的事件。如果是静态事件则不需要for附加项 DO 11 TIMES. CALL METHOD o_vehicle->accelerate. CALL METHOD o_vehicle->show_speed. ENDDO.