• ABAP ObjectOriented Programming


    引用:翱翔云天

    203 views

    介绍abap object,从最基础的开始,循序渐进,慢慢掌握。

    从一个最简单的例子讲起,尽量把object语句都能够涉及到。这个例子我们建了一个类,里面有相应的属性以及方法。

    问题假设:有一个对象actor,对其名字进行操作,包括first name和last name,通过constructor进行初始化,利用方法ToString显示最终结果。

    REPORT ZBOBO_OO_001 .

    *-----------------Class definition------------------------*

    *Syntax

    * CLASS cl DEFINITION [PUBLIC][INHERITING FROM super]

    * [ABSTRACT][FINAL]

    * [CREATE {PUBLIC|PROCTECTED|PRIVATE}]

    * [[GLOBAL] FRIENDS [class][ifac]].

    * [PUBLIC SECTION].

    * This section defines the public visibility area of

    * class,all components in this area can be addressed

    * from outside the class, and it's subclass,and within

    * the class itself.

    * [PRIVATE SECTION].

    * The components in this area can be addressed only in

    * this class itself.

    * [PROTECTED SECTION].

    * The components in this area can be addressed in this

    * class itself and the subclass of the class.

    * ENDCLASS.

    *-----------------Class definition------------------------*

    ClASS LCL_ACTOR DEFINITION.

    PUBLIC SECTION.

    *----------------Methods definition-----------------------*

    *Syntax

    * General method

    * METHODS meth [ABSTRACT|FINAL]

    * [IMPORTING {parameter} [PREFERRED PARAMETER p]]

    * [EXPORTING {parameter}]

    * [CHANGING {parameter}]

    * [{RAISING|EXCEPTIONS} {exc}].

    * Functional method

    * METHODS meth [ABSTRACT|FINAL]

    * [IMPORTING {parameter} [PREFERRED PARAMETER p]]

    * RETURNING VALUE(v) {TYPE ...}|{LIKE ...}

    * [{RAISING|EXCEPTIONS} {exc}].

    *----------------Methods definition-----------------------*

    METHODS:

    * Initialize the first name and last name

    CONSTRUCTOR

    IMPORTING SN TYPE STRING,

    * Get first name

    GETNAME

    RETURNING VALUE(NA) TYPE STRING,

    * Set first name

    SETNAME

    IMPORTING N TYPE STRING,

    * Get last name

    GETSTAGENAME

    RETURNING VALUE(STN) TYPE STRING,

    * Set last name

    SETSTAGENAME

    IMPORTING SN TYPE STRING,

    * Show result

    TOSTRING

    RETURNING VALUE(STR) TYPE STRING.

    PRIVATE SECTION.

    DATA:

    NAME TYPE STRING, "First name

    STAGENAME TYPE STRING. "Last name

    ENDCLASS.

    *-----------------Class implementation-------------------*

    *Syntax

    * CLASS c1 IMPLEMENTATION.

    * ...

    * METHOD ...

    * ...

    * ENDMETHOD.

    * ...

    * ENDCLASS.

    *-----------------Class implementation-------------------*

    CLASS LCL_ACTOR IMPLEMENTATION.

    METHOD CONSTRUCTOR.

    NAME = '<NONE>'.

    STAGENAME = SN.

    ENDMETHOD.

    METHOD GETNAME.

    NA = NAME.

    ENDMETHOD.

    METHOD SETNAME.

    NAME = N.

    ENDMETHOD.

    METHOD GETSTAGENAME.

    STN = STAGENAME.

    ENDMETHOD.

    METHOD SETSTAGENAME.

    STAGENAME = SN.

    ENDMETHOD.

    METHOD TOSTRING.

    DATA: L1(20) TYPE C,

    L2(20) TYPE C.

    *---The below two statement is the same---*

    * CALL METHOD GETSTAGENAME

    * RECEIVING STN = L1.

    COMPUTE L1 = ME->GETSTAGENAME( ).

    *---The below two statement is the same---*

    CALL METHOD GETNAME

    RECEIVING NA = L2.

    CONCATENATE 'First name is:'

    L1

    ',and last name is:'

    L2

    INTO STR.

    ENDMETHOD.

    ENDCLASS.

    *-----------------Main program-------------------*

    DATA: OREF TYPE REF TO LCL_ACTOR.

    DATA: INT TYPE STRING,

    1ST TYPE STRING,

    LST TYPE STRING,

    RESULT TYPE STRING.

    START-OF-SELECTION.

    INT = 'XU BOBO'.

    1ST = 'JAME'.

    LST = 'GREEN'.

    * Initialize

    CREATE OBJECT OREF

    EXPORTING SN = INT.

    CALL METHOD OREF->TOSTRING

    RECEIVING STR = RESULT.

    WRITE: / RESULT.

    * Set last name

    CALL METHOD OREF->SETNAME

    EXPORTING N = 1ST.

    CALL METHOD OREF->TOSTRING

    RECEIVING STR = RESULT.

    WRITE: / RESULT.

    * Set fist name

    CALL METHOD OREF->SETSTAGENAME

    EXPORTING SN = LST.

    CALL METHOD OREF->TOSTRING

    RECEIVING STR = RESULT.

    WRITE: / RESULT.

    Basic methods declaration and implementation

    REPORT zbobo_oo_002 .

    *-----------------Description------------------------*

    *Basic methods declaration and implementation

    *-----------------Description------------------------*

    * Class declarations

    CLASS attributes_and_methods DEFINITION.

    PUBLIC SECTION.

    CLASS-DATA object_static TYPE i.

    METHODS:

    set_object_value

    IMPORTING value(i_object_value) TYPE i,

    get_object_value

    EXPORTING value(e_object_value) TYPE i,

    show_result.

    PRIVATE SECTION.

    DATA object_value TYPE i.

    CLASS-DATA object_count TYPE i.

    ENDCLASS.

    *Class implementations

    CLASS attributes_and_methods IMPLEMENTATION.

    METHOD set_object_value.

    object_value = i_object_value.

    ENDMETHOD.

    METHOD get_object_value.

    e_object_value = object_value.

    object_static = object_value.

    ENDMETHOD.

    METHOD show_result.

    CALL METHOD me->get_object_value

    IMPORTING e_object_value = object_count.

    WRITE: / object_count.

    ENDMETHOD.

    ENDCLASS.

    DATA val TYPE i.

    DATA oref TYPE REF TO attributes_and_methods.

    START-OF-SELECTION.

    CREATE OBJECT oref.

    val = 10.

    CALL METHOD oref->set_object_value

    EXPORTING i_object_value = val.

    CALL METHOD oref->show_result.

    * The static attribute of class can be accessed directly

    * via => symbol

    WRITE: attributes_and_methods=>object_static.

    Example of self-reference

    REPORT zbobo_oo_003 .

    *-----------------Description------------------------*

    * Example of self-reference

    *-----------------Description------------------------*

    *Class declaration

    CLASS client DEFINITION.

    PUBLIC SECTION.

    DATA name(10) TYPE c VALUE 'Master' READ-ONLY.

    METHODS create_server.

    ENDCLASS.

    *---------------------------------------------------------------------*

    * CLASS server DEFINITION

    *---------------------------------------------------------------------*

    * ........ *

    *---------------------------------------------------------------------*

    CLASS server DEFINITION.

    PUBLIC SECTION.

    METHODS acknowledge

    IMPORTING creator TYPE REF TO client.

    PRIVATE SECTION.

    DATA name(10) TYPE c VALUE 'servant'.

    ENDCLASS.

    *Class implementation

    CLASS client IMPLEMENTATION.

    METHOD create_server.

    DATA server_ref TYPE REF TO server.

    CREATE OBJECT server_ref.

    CALL METHOD server_ref->acknowledge

    EXPORTING creator = me.

    ENDMETHOD.

    ENDCLASS.

    *---------------------------------------------------------------------*

    * CLASS server IMPLEMENTATION

    *---------------------------------------------------------------------*

    * ........ *

    *---------------------------------------------------------------------*

    CLASS server IMPLEMENTATION.

    METHOD acknowledge.

    DATA name TYPE string.

    name = creator->name.

    WRITE: me->name, 'created by', name.

    ENDMETHOD.

    ENDCLASS.

    *Global data

    DATA client_ref TYPE REF TO client.

    *Processing blocks

    START-OF-SELECTION.

    CREATE OBJECT client_ref.

    CALL METHOD client_ref->create_server.

    Dynamic method call

    REPORT zbobo_oo_004 .

    *-----------------Description------------------------*

    * Dynamic method call

    *-----------------Description------------------------*

    *For dynamic call, using system object

    CLASS cl_abap_objectdescr DEFINITION LOAD.

    *Class declaration

    CLASS table_content DEFINITION.

    PUBLIC SECTION.

    TYPES:

    BEGIN OF tr_vbeln,

    sign(1),

    option(2),

    low TYPE vbeln_va,

    high TYPE vbeln_va,

    END OF tr_vbeln.

    TYPES:

    it_rang TYPE TABLE OF tr_vbeln.

    METHODS:

    get_vbak IMPORTING i_vbeln TYPE it_rang,

    get_vbap IMPORTING i_vbeln TYPE it_rang.

    PRIVATE SECTION.

    DATA:

    wa_vbak TYPE vbak,

    it_vbak TYPE TABLE OF vbak,

    wa_vbap TYPE vbap,

    it_vbap TYPE TABLE OF vbap.

    DATA:

    tbname TYPE string.

    METHODS show_result

    IMPORTING tb_name TYPE string.

    ENDCLASS.

    *Class implementation

    CLASS table_content IMPLEMENTATION.

    METHOD get_vbak.

    SELECT * FROM vbak INTO TABLE it_vbak

    WHERE vbeln IN i_vbeln.

    tbname = 'VBAK'.

    CALL METHOD show_result

    EXPORTING tb_name = tbname.

    ENDMETHOD.

    METHOD get_vbap.

    SELECT * FROM vbap INTO TABLE it_vbap

    WHERE vbeln IN i_vbeln.

    tbname = 'VBAP'.

    CALL METHOD show_result

    EXPORTING tb_name = tbname.

    ENDMETHOD.

    METHOD show_result.

    CASE tb_name.

    WHEN 'VBAK'.

    LOOP AT it_vbak INTO wa_vbak.

    WRITE: / wa_vbak-vbeln.

    ENDLOOP.

    WHEN 'VBAP'.

    LOOP AT it_vbap INTO wa_vbap.

    WRITE: / wa_vbap-vbeln, wa_vbap-posnr.

    ENDLOOP.

    ENDCASE.

    ENDMETHOD.

    ENDCLASS.

    DATA:

    oref TYPE REF TO table_content,

    dv TYPE vbeln_va.

    * A new way for dynamic call in ABAP OO

    DATA:

    ptab TYPE abap_parmbind_tab,

    ptab_line TYPE abap_parmbind.

    PARAMETERS:

    t_name(10) TYPE c.

    SELECT-OPTIONS:

    p_vbeln FOR dv.

    START-OF-SELECTION.

    CREATE OBJECT oref.

    CONCATENATE 'GET_' t_name INTO t_name.

    * First way for dynamic call, just like our function call or form call

    CATCH SYSTEM-EXCEPTIONS dyn_call_meth_not_found = 4.

    CALL METHOD oref->(t_name)

    EXPORTING i_vbeln = p_vbeln[].

    ENDCATCH.

    IF sy-subrc = 4.

    WRITE: 'Wrong method name!'.

    ENDIF.

    * Second way for dynamic call, it is special in ABAP OO

    ptab_line-name = 'I_VBELN'.

    ptab_line-kind = cl_abap_objectdescr=>exporting.

    GET REFERENCE OF p_vbeln[] INTO ptab_line-value.

    * ptab_line-value = 'P_VBELN[]'.

    INSERT ptab_line INTO TABLE ptab.

    WRITE: / 'New dynamic call in ABAP OO:'.

    CALL METHOD oref->(t_name) PARAMETER-TABLE ptab.

    Widening cast

    REPORT zbobo_oo_005 .

    *-----------------Description------------------------*

    * Widening cast

    *-----------------Description------------------------*

    *Class declaration

    CLASS vehicle DEFINITION.

    ENDCLASS.

    *---------------------------------------------------------------------*

    * CLASS plane DEFINITION

    *---------------------------------------------------------------------*

    * ........ *

    *---------------------------------------------------------------------*

    CLASS plane DEFINITION INHERITING FROM vehicle.

    ENDCLASS.

    *---------------------------------------------------------------------*

    * CLASS ship DEFINITION

    *---------------------------------------------------------------------*

    * ........ *

    *---------------------------------------------------------------------*

    CLASS ship DEFINITION INHERITING FROM vehicle.

    ENDCLASS.

    *---------------------------------------------------------------------*

    * CLASS vehicle_handler DEFINITION

    *---------------------------------------------------------------------*

    * ........ *

    *---------------------------------------------------------------------*

    CLASS vehicle_handler DEFINITION.

    PUBLIC SECTION.

    METHODS handle_vehicle

    IMPORTING i_ref TYPE REF TO object.

    ENDCLASS.

    *class implementation

    CLASS vehicle_handler IMPLEMENTATION.

    METHOD handle_vehicle.

    DATA:

    vehicle_ref TYPE REF TO vehicle,

    plane_ref TYPE REF TO plane,

    ship_ref TYPE REF TO ship.

    CATCH SYSTEM-EXCEPTIONS move_cast_error = 4.

    vehicle_ref ?= i_ref.

    ENDCATCH.

    IF sy-subrc = 0.

    WRITE / 'WOrking with vehicle'.

    ELSE.

    WRITE / 'this is not a vehicle'.

    EXIT.

    ENDIF.

    CATCH SYSTEM-EXCEPTIONS move_cast_error = 4.

    plane_ref ?= i_ref.

    ENDCATCH.

    IF sy-subrc = 0.

    WRITE / 'WOrking with plane'.

    ENDIF.

    CATCH SYSTEM-EXCEPTIONS move_cast_error = 4.

    ship_ref ?= i_ref.

    ENDCATCH.

    IF sy-subrc = 0.

    WRITE / 'WOrking with ship'.

    ENDIF.

    ENDMETHOD.

    ENDCLASS.

    DATA:

    handler TYPE REF TO vehicle_handler,

    vehicle_1 TYPE REF TO vehicle,

    vehicle_2 LIKE vehicle_1.

    START-OF-SELECTION.

    CREATE OBJECT:

    handler,

    vehicle_1 TYPE plane,

    vehicle_2 TYPE ship.

    CALL METHOD:

    handler->handle_vehicle

    EXPORTING i_ref = handler,

    handler->handle_vehicle

    EXPORTING i_ref = vehicle_1,

    handler->handle_vehicle

    EXPORTING i_ref = vehicle_2.

    Example of polymorphism through inheritance

    REPORT zbobo_oo_006 .

    *-----------------Description------------------------*

    * Example of polymorphism through inheritance

    *-----------------Description------------------------*

    *Class declaration

    CLASS ship DEFINITION ABSTRACT.

    PUBLIC SECTION.

    METHODS:

    get_max_range ABSTRACT

    RETURNING value(r_range) TYPE f,

    get_name

    RETURNING value(r_name) TYPE string.

    PROTECTED SECTION.

    DATA name TYPE string.

    ENDCLASS.

    *---------------------------------------------------------------------*

    * CLASS motorship DEFINITION

    *---------------------------------------------------------------------*

    * ........ *

    *---------------------------------------------------------------------*

    CLASS motorship DEFINITION INHERITING FROM ship.

    PUBLIC SECTION.

    METHODS:

    constructor IMPORTING

    value(i_name) TYPE string

    value(i_fuel_consumption) TYPE f

    value(i_fuel_amount) TYPE f,

    get_max_range REDEFINITION.

    PROTECTED SECTION.

    DATA:

    fuel_consumption TYPE f,

    fuel_amount TYPE f.

    ENDCLASS.

    *---------------------------------------------------------------------*

    * CLASS sailingship DEFINITION

    *---------------------------------------------------------------------*

    * ........ *

    *---------------------------------------------------------------------*

    CLASS sailingship DEFINITION INHERITING FROM ship.

    PUBLIC SECTION.

    METHODS: constructor IMPORTING

    value(i_name) TYPE string

    value(i_range_per_wind_speed) TYPE f

    value(i_wind_speed) TYPE f

    value(i_wind_duration) TYPE f,

    get_max_range REDEFINITION.

    PROTECTED SECTION.

    DATA: range_per_wind_speed TYPE f,

    wind_speed TYPE f,

    wind_duration TYPE f.

    ENDCLASS.

    *Class implementation

    CLASS ship IMPLEMENTATION.

    METHOD get_name.

    r_name = name.

    ENDMETHOD.

    ENDCLASS.

    *---------------------------------------------------------------------*

    * CLASS motorship IMPLEMENTATION

    *---------------------------------------------------------------------*

    * ........ *

    *---------------------------------------------------------------------*

    CLASS motorship IMPLEMENTATION.

    METHOD constructor.

    name = i_name.

    fuel_consumption = i_fuel_consumption.

    fuel_amount = i_fuel_amount.

    ENDMETHOD.

    METHOD get_max_range.

    r_range = fuel_amount / fuel_consumption.

    ENDMETHOD.

    ENDCLASS.

    *---------------------------------------------------------------------*

    * CLASS sailingship IMPLEMENTATION

    *---------------------------------------------------------------------*

    * ........ *

    *---------------------------------------------------------------------*

    CLASS sailingship IMPLEMENTATION.

    METHOD constructor.

    name = i_name.

    range_per_wind_speed = i_range_per_wind_speed.

    wind_speed = i_wind_speed.

    wind_duration = i_wind_duration.

    ENDMETHOD.

    METHOD get_max_range.

    r_range = wind_speed /

    range_per_wind_speed * wind_duration.

    ENDMETHOD.

    ENDCLASS.

    *Global data

    DATA:

    o_ship TYPE REF TO ship,

    ship_tab TYPE TABLE OF REF TO ship,

    name TYPE string,

    range TYPE f.

    START-OF-SELECTION.

    CREATE OBJECT o_ship TYPE motorship

    EXPORTING i_name = 'Motor Ship Wanda'

    i_fuel_consumption = '0.2'

    i_fuel_amount = 80.

    APPEND o_ship TO ship_tab.

    CREATE OBJECT o_ship TYPE sailingship

    EXPORTING i_name = 'Sailing Ship Black Magic'

    i_range_per_wind_speed = 25

    i_wind_speed = 60

    i_wind_duration = 5.

    APPEND o_ship TO ship_tab.

    LOOP AT ship_tab INTO o_ship.

    name = o_ship->get_name( ).

    range = o_ship->get_max_range( ).

    WRITE: / 'Range of', name,'is',

    range EXPONENT 0 DECIMALS 2,'Miles'.

    ENDLOOP.

    Narrowing cast for interface reference

    REPORT zbobo_oo_007 .

    *-----------------Description------------------------*

    * Narrowing cast for interface reference

    *-----------------Description------------------------*

    *Interfaces

    INTERFACE i1.

    METHODS meth.

    ENDINTERFACE.

    *---------------------------------------------------------------------*

    * INTERFACE i2

    *---------------------------------------------------------------------*

    * ........ *

    *---------------------------------------------------------------------*

    INTERFACE i2.

    INTERFACES i1.

    METHODS meth.

    ENDINTERFACE.

    *Class declaration

    CLASS c1 DEFINITION.

    PUBLIC SECTION.

    INTERFACES i2.

    ENDCLASS.

    *Class implementation

    CLASS c1 IMPLEMENTATION.

    METHOD i1~meth.

    WRITE: / 'Method of interface i1'.

    ENDMETHOD.

    METHOD i2~meth.

    WRITE: / 'Method of interface i2'.

    ENDMETHOD.

    ENDCLASS.

    DATA:

    iref1 TYPE REF TO i1,

    iref2 TYPE REF TO i2.

    START-OF-SELECTION.

    * Narrowing cast part

    CREATE OBJECT iref2 TYPE c1.

    * Narrowing cast part

    * Narrowing cast part

    iref1 = iref2.

    * Narrowing cast part

    CALL METHOD iref1->meth.

    CALL METHOD iref2->i1~meth.

    * Same as below because of narrowing cast

    CALL METHOD iref2->meth.

    Interface reference widening cast

    REPORT zbobo_oo_008 .

    *-----------------Description------------------------*

    * Interface reference widening cast

    *-----------------Description------------------------*

    *Interface declaration

    INTERFACE object_status.

    METHODS write_status.

    ENDINTERFACE.

    *---------------------------------------------------------------------*

    * INTERFACE object_movement

    *---------------------------------------------------------------------*

    * ........ *

    *---------------------------------------------------------------------*

    INTERFACE object_movement.

    METHODS accelerate.

    ENDINTERFACE.

    *Class declaration

    CLASS vehicle DEFINITION.

    PUBLIC SECTION.

    INTERFACES:

    object_status,

    object_movement.

    PROTECTED SECTION.

    DATA speed TYPE i.

    ENDCLASS.

    *Class implementation

    CLASS vehicle IMPLEMENTATION.

    METHOD object_movement~accelerate.

    speed = speed + 1.

    ENDMETHOD.

    METHOD object_status~write_status.

    WRITE: / 'Speed', speed.

    ENDMETHOD.

    ENDCLASS.

    DATA:

    movement_ref TYPE REF TO object_movement,

    status_ref TYPE REF TO object_status.

    START-OF-SELECTION.

    * Narrowing cast

    CREATE OBJECT movement_ref TYPE vehicle.

    CALL METHOD movement_ref->accelerate.

    * Widening cast

    status_ref ?= movement_ref.

    CALL METHOD status_ref->write_status.

    Polymorphism through interfaces

    REPORT zbobo_oo_009 .

    *-----------------Description------------------------*

    * polymorphism through interfaces

    *-----------------Description------------------------*

    *Interfaces delaration

    INTERFACE object_status.

    METHODS write_status.

    ENDINTERFACE.

    *Class delaration

    CLASS window DEFINITION.

    PUBLIC SECTION.

    METHODS open_close.

    INTERFACES object_status.

    PROTECTED SECTION.

    DATA status_flag(1) TYPE c.

    ENDCLASS.

    *---------------------------------------------------------------------*

    * CLASS vehicle DEFINITION

    *---------------------------------------------------------------------*

    * ........ *

    *---------------------------------------------------------------------*

    CLASS vehicle DEFINITION.

    PUBLIC SECTION.

    INTERFACES object_status.

    METHODS accelerate.

    PROTECTED SECTION.

    DATA speed TYPE i.

    ENDCLASS.

    *---------------------------------------------------------------------*

    * CLASS window IMPLEMENTATION

    *---------------------------------------------------------------------*

    * ........ *

    *---------------------------------------------------------------------*

    CLASS window IMPLEMENTATION.

    METHOD open_close.

    IF status_flag = ''.

    status_flag = 'X'.

    ELSE.

    status_flag = ''.

    ENDIF.

    ENDMETHOD.

    METHOD object_status~write_status.

    IF status_flag = ''.

    WRITE: / 'Window is closed'.

    ELSE.

    WRITE: / 'Window is open'.

    ENDIF.

    ENDMETHOD.

    ENDCLASS.

    *---------------------------------------------------------------------*

    * CLASS vehicle IMPLEMENTATION

    *---------------------------------------------------------------------*

    * ........ *

    *---------------------------------------------------------------------*

    CLASS vehicle IMPLEMENTATION.

    METHOD accelerate.

    speed = speed + 1.

    ENDMETHOD.

    METHOD object_status~write_status.

    WRITE: / 'Speed', speed.

    ENDMETHOD.

    ENDCLASS.

    DATA:

    vehicle_ref TYPE REF TO vehicle,

    window_ref TYPE REF TO window,

    status_tab TYPE TABLE OF REF TO object_status.

    FIELD-SYMBOLS <status> TYPE REF TO object_status.

    START-OF-SELECTION.

    CREATE OBJECT vehicle_ref.

    CALL METHOD vehicle_ref->accelerate.

    APPEND vehicle_ref TO status_tab.

    CREATE OBJECT window_ref.

    CALL METHOD window_ref->open_close.

    APPEND window_ref TO status_tab.

    LOOP AT status_tab ASSIGNING <status>.

    CALL METHOD <status>->write_status.

    ENDLOOP.

    Definition and triggering of events and handle events

    REPORT zbobo_oo_010 .

    *-----------------Description------------------------*

    * Definition and triggering of events

    * And handle events

    *-----------------Description------------------------*

    *Class declaration

    CLASS pilot DEFINITION.

    PUBLIC SECTION.

    METHODS call_flight_attendant.

    EVENTS call_button_pressed.

    ENDCLASS.

    *---------------------------------------------------------------------*

    * CLASS passenger DEFINITION

    *---------------------------------------------------------------------*

    * ........ *

    *---------------------------------------------------------------------*

    CLASS passenger DEFINITION.

    PUBLIC SECTION.

    METHODS: constructor

    IMPORTING i_seatnumber TYPE i,

    call_for_help.

    EVENTS call_button_pressed

    EXPORTING value(e_seatnumber) TYPE i.

    PROTECTED SECTION.

    DATA seatnumber TYPE i.

    ENDCLASS.

    *---------------------------------------------------------------------*

    * CLASS flight_attendant DEFINITION

    *---------------------------------------------------------------------*

    * ........ *

    *---------------------------------------------------------------------*

    CLASS flight_attendant DEFINITION.

    PUBLIC SECTION.

    METHODS: constructor

    IMPORTING i_id TYPE string,

    help_the_pilot FOR EVENT

    call_button_pressed OF pilot,

    help_the_passenger FOR EVENT

    call_button_pressed OF passenger

    IMPORTING e_seatnumber.

    PROTECTED SECTION.

    DATA id TYPE string.

    ENDCLASS.

    *Class implementation

    CLASS pilot IMPLEMENTATION.

    METHOD call_flight_attendant.

    RAISE EVENT call_button_pressed.

    ENDMETHOD.

    ENDCLASS.

    *---------------------------------------------------------------------*

    * CLASS passenger IMPLEMENTATION

    *---------------------------------------------------------------------*

    * ........ *

    *---------------------------------------------------------------------*

    CLASS passenger IMPLEMENTATION.

    METHOD constructor.

    seatnumber = i_seatnumber.

    ENDMETHOD.

    METHOD call_for_help.

    RAISE EVENT call_button_pressed

    EXPORTING e_seatnumber = seatnumber.

    ENDMETHOD.

    ENDCLASS.

    *---------------------------------------------------------------------*

    * CLASS flight_attendant IMPLEMENTATION

    *---------------------------------------------------------------------*

    * ........ *

    *---------------------------------------------------------------------*

    CLASS flight_attendant IMPLEMENTATION.

    METHOD constructor.

    id = i_id.

    ENDMETHOD.

    METHOD help_the_pilot.

    WRITE: / id, 'helps pilot'.

    ENDMETHOD.

    METHOD help_the_passenger.

    WRITE: / id, 'helps passenger on seat',e_seatnumber.

    ENDMETHOD.

    ENDCLASS.

    DATA: pilot TYPE REF TO pilot,

    passenger_1 TYPE REF TO passenger,

    passenger_2 TYPE REF TO passenger.

    DATA: purser TYPE REF TO flight_attendant,

    stewardess TYPE REF TO flight_attendant.

    START-OF-SELECTION.

    CREATE OBJECT:

    pilot,

    passenger_1 EXPORTING i_seatnumber = 11,

    passenger_2 EXPORTING i_seatnumber = 17,

    purser EXPORTING i_id = 'Purser',

    stewardess EXPORTING i_id = 'Stewardess'.

    SET HANDLER:

    purser->help_the_pilot FOR pilot,

    stewardess->help_the_passenger FOR ALL INSTANCES.

    CALL METHOD:

    pilot->call_flight_attendant,

    passenger_1->call_for_help,

    passenger_2->call_for_help.

  • 相关阅读:
    数据表与简单Java类(多对多的关系)
    数据表与简单java类(一对多的关系)
    微信二次开发SDK使用教程--手机朋友圈评论删除任务反馈通知服务端
    微信二次开发SDK使用教程--手机检测到有人评论/删除朋友圈通知服务端
    微信二次开发SDK使用教程--手机检测到有人点赞/取消点赞通知服务端
    微信二次开发sdk使用教程--手机上删除朋友圈通知服务端
    微信二次开发sdk使用教程--手机上发送了朋友圈通知服务端
    微信二次开发SDK教程--上传手机客户端上微信的二维码给服务端
    微信开发SDK使用教程--手机微信个人号移除好友通知服务端
    微信开发SDK使用教程--手机微信个人号新增好友通知服务端
  • 原文地址:https://www.cnblogs.com/wequst/p/1513459.html
Copyright © 2020-2023  润新知