二维码

[面向对象] ABAP面向对象中的集合与一般调用

Twilight发表于 2016-04-20 09:51Twilight 最后回复于 2016-04-20 09:51 [复制链接] 4020 0

  1. REPORT yleon_002.

  2. TYPE-POOLS icon.

  3. INCLUDE yleon_002_a.
  4. INCLUDE yleon_002_b.

  5. DATA: r_plane     TYPE REF TO lcl_airplane,
  6.       r_cargo     TYPE REF TO lcl_cargo_plane,
  7.       r_passenger TYPE REF TO lcl_passenger_plane,
  8.       plane_list  TYPE TABLE OF REF TO lcl_airplane,
  9.       r_carrier   TYPE REF TO lcl_carrier.

  10. START-OF-SELECTION.

  11. ***** Create Carrier ********************************************
  12. CREATE OBJECT r_carrier EXPORTING im_name = 'Smile&Fly Travel'.
  13. ***** Passenger Plane ********************************************
  14. CREATE OBJECT r_passenger EXPORTING
  15. im_name = 'LH BERLIN'
  16. im_planetype = '747-400'
  17. im_seats = 345.
  18. ***** cargo Plane ************************************************
  19. CREATE OBJECT r_cargo EXPORTING
  20. im_name = 'US HErcules'
  21. im_planetype = '747-500'
  22. im_cargo = 533.
  23. ***** insert planes into itab if client ***************************
  24. r_carrier->add_airplane( r_passenger ).
  25. r_carrier->add_airplane( r_cargo ).
  26. ***** show all airplanes inside carrier ***************************
  27. r_carrier->display_attributes( ).
复制代码
  1. *&---------------------------------------------------------------------*
  2. *&  包含                YLEON_002_A
  3. *&---------------------------------------------------------------------*
  4. CLASS lcl_airplane DEFINITION.
  5.   PUBLIC SECTION.
  6.     CONSTANTS: pos_1 TYPE i VALUE 30.
  7.     METHODS: constructor IMPORTING im_name      TYPE string
  8.                                    im_planetype TYPE saplane-planetype,
  9.       display_attributes.
  10.     CLASS-METHODS: display_n_o_airplanes.

  11.   PRIVATE SECTION.
  12.     METHODS: get_technical_attributes
  13.       IMPORTING im_type    TYPE saplane-planetype
  14.       EXPORTING ex_weight  TYPE s_plan_wei
  15.                 ex_tankcap TYPE s_capacity.
  16.     DATA: name      TYPE string,
  17.           planetype TYPE saplane-planetype.
  18.     CLASS-DATA: n_o_airplanes TYPE i.
  19. ENDCLASS. "lcl_airplane DEFINITION

  20. CLASS lcl_airplane IMPLEMENTATION.
  21.   METHOD constructor.
  22.     name = im_name.
  23.     planetype = im_planetype.
  24.     n_o_airplanes = n_o_airplanes + 1.
  25.   ENDMETHOD. "constructor
  26.   METHOD display_attributes.
  27.     DATA: weight TYPE saplane-weight,
  28.           cap    TYPE saplane-tankcap.
  29.     WRITE: / icon_ws_plane AS ICON,
  30.     / 'Name of airplane'(001), AT pos_1 name,
  31.     / 'Airplane type: '(002), AT pos_1 planetype.
  32.     get_technical_attributes( EXPORTING im_type = planetype
  33.                               IMPORTING ex_weight = weight
  34.                               ex_tankcap = cap ).
  35.     WRITE: / 'Weight:'(003), weight,
  36.     'Tank capacity:'(004), cap.
  37.   ENDMETHOD. "display_attributes
  38.   METHOD display_n_o_airplanes.
  39.     WRITE: /, / 'Number of airplanes: '(ca1),
  40.     AT pos_1 n_o_airplanes LEFT-JUSTIFIED, /.
  41.   ENDMETHOD. "display_n_o_airplanes
  42.   METHOD get_technical_attributes.
  43.     SELECT SINGLE weight tankcap FROM saplane
  44.     INTO (ex_weight, ex_tankcap)
  45.     WHERE planetype = im_type.
  46.     IF sy-subrc <> 0.
  47.       ex_weight = 100000.
  48.       ex_tankcap = 10000.
  49.     ENDIF.
  50.   ENDMETHOD. "get_technical_attributes
  51. ENDCLASS. "lcl_airplane IMPLEMENTATION

  52. CLASS lcl_cargo_plane DEFINITION INHERITING FROM lcl_airplane.
  53.   PUBLIC SECTION.
  54.     METHODS: constructor IMPORTING im_name      TYPE string
  55.                                    im_planetype TYPE saplane-planetype
  56.                                    im_cargo     TYPE scplane-cargomax.
  57.     METHODS: display_attributes REDEFINITION.
  58.     METHODS: get_cargo RETURNING VALUE(re_cargo) TYPE scplane-cargomax.
  59.   PRIVATE SECTION.
  60.     DATA: max_cargo TYPE scplane-cargomax.
  61. ENDCLASS. "lcl_cargo_plane DEFINITION
  62. CLASS lcl_cargo_plane IMPLEMENTATION.
  63.   METHOD constructor.
  64.     CALL METHOD super->constructor( im_name = im_name im_planetype = im_planetype ).
  65.     max_cargo = im_cargo.
  66.   ENDMETHOD. "constructor
  67.   METHOD display_attributes.
  68.     super->display_attributes( ).
  69.     WRITE: / 'Max Cargo = ', max_cargo.
  70.     ULINE.
  71.   ENDMETHOD. "display_attributes
  72.   METHOD get_cargo.
  73.     re_cargo = max_cargo.
  74.   ENDMETHOD. "get_cargo
  75. ENDCLASS. "lcl_cargo_plane IMPLEMENTATION
  76. CLASS lcl_passenger_plane DEFINITION INHERITING FROM lcl_airplane..
  77.   PUBLIC SECTION.
  78.     METHODS: constructor IMPORTING im_name      TYPE string
  79.                                    im_planetype TYPE saplane-planetype
  80.                                    im_seats     TYPE sflight-seatsmax.
  81.     METHODS: display_attributes REDEFINITION.
  82.   PRIVATE SECTION.
  83.     DATA: max_seats TYPE sflight-seatsmax.
  84. ENDCLASS. "lcl_passenger_plane DEFINITION

  85. CLASS lcl_passenger_plane IMPLEMENTATION.
  86.   METHOD constructor.
  87.     CALL METHOD super->constructor(
  88.       EXPORTING
  89.         im_name      = im_name
  90.         im_planetype = im_planetype ).
  91.     max_seats = im_seats.
  92.   ENDMETHOD. "constructor
  93.   METHOD display_attributes.
  94.     super->display_attributes( ).
  95.     WRITE: / 'Max Seats = ', max_seats.
  96.     ULINE.
  97.   ENDMETHOD. "display_attributes
  98. ENDCLASS. "lcl_passenger_plane IMPLEMENTATION
复制代码
  1. *&---------------------------------------------------------------------*
  2. *&  包含                YLEON_002_B
  3. *&---------------------------------------------------------------------*
  4. CLASS lcl_carrier DEFINITION.
  5.   PUBLIC SECTION.
  6.     METHODS: constructor IMPORTING im_name TYPE string,
  7.       get_name RETURNING VALUE(ex_name) TYPE string,
  8.       add_airplane IMPORTING im_plane TYPE REF TO lcl_airplane,
  9.       display_airplanes,
  10.       display_attributes,
  11.       get_highest_cargo RETURNING VALUE(re_cargo) TYPE s_plan_car.
  12.   PRIVATE SECTION.
  13.     DATA: name          TYPE string,
  14.           airplane_list TYPE TABLE OF REF TO lcl_airplane.
  15. ENDCLASS. "lcl_carrier DEFINITION
  16. CLASS lcl_carrier IMPLEMENTATION.
  17.   METHOD add_airplane.
  18.     APPEND im_plane TO airplane_list.
  19.   ENDMETHOD. "add_airplane
  20.   METHOD display_attributes.
  21.     DATA: highest_cargo TYPE s_plan_car.
  22.     WRITE: icon_flight AS ICON, name . ULINE. ULINE.
  23.     display_airplanes( ).
  24.     highest_cargo = me->get_highest_cargo( ).
  25.     WRITE: / ' Highest Cargo = ', highest_cargo.
  26.   ENDMETHOD. "display_attributes
  27.   METHOD display_airplanes.
  28.     DATA: r_plane TYPE REF TO lcl_airplane.
  29.     LOOP AT airplane_list INTO r_plane.
  30.       r_plane->display_attributes( ).
  31.     ENDLOOP.
  32.   ENDMETHOD. "display_airplanes
  33.   METHOD constructor.
  34.     name = im_name.
  35.   ENDMETHOD. "constructor
  36.   METHOD get_name.
  37.     ex_name = name.
  38.   ENDMETHOD. "get_name
  39.   METHOD get_highest_cargo.
  40.     DATA: r_cargo TYPE REF TO lcl_cargo_plane.
  41.     DATA: r_plane TYPE REF TO lcl_airplane.
  42.     DATA: cargo TYPE s_plan_car.
  43.     DATA: r_exc TYPE REF TO cx_root.
  44.     LOOP AT airplane_list INTO r_plane.
  45.       TRY.
  46.           "**** her comes the optimistic coding *******
  47.           r_cargo ?= r_plane.
  48.           cargo = r_cargo->get_cargo( ).
  49.           IF re_cargo < cargo.
  50.             re_cargo = cargo.
  51.           ENDIF.
  52.         CATCH cx_sy_move_cast_error INTO r_exc. "*** no cargoplane
  53.       ENDTRY.
  54.     ENDLOOP.
  55.   ENDMETHOD.
  56. ENDCLASS. "lcl_carrier IMPLEMENTATION
复制代码

执行结果:
集合与一般调用.jpg
回复

使用道具 举报

快速回帖

本版积分规则
您需要登录后才可以回帖 登录 | 注册有礼

快速回复 返回顶部 返回列表