二维码

[面向对象] ABAP面向对象中方法调用

Twilight发表于 2016-04-13 11:14Twilight 最后回复于 2016-04-13 11:14 [复制链接] 3380 0

静态方法直接输出:lcl_airplane=>display_n_o_airplanes( ).
静态功能性返回参数输出:count = lcl_airplane=>get_n_o_airplanes( ).

  1. REPORT yleon_001.

  2. INCLUDE yleon_001_airs_a.

  3. DATA: r_plane    TYPE REF TO lcl_airplane,
  4.       plane_list TYPE TABLE OF REF TO lcl_airplane,
  5.       count      TYPE i.

  6. START-OF-SELECTION.

  7.   lcl_airplane=>display_n_o_airplanes( ).
  8.   CREATE OBJECT r_plane.
  9.   APPEND r_plane TO plane_list.
  10.   r_plane->set_attributes( im_name = 'LH Berlin'
  11.   im_planetype = 'A321' ).
  12.   CREATE OBJECT r_plane.
  13.   APPEND r_plane TO plane_list.
  14.   r_plane->set_attributes( im_name = 'AA New York'
  15.   im_planetype = '747-400' ).
  16.   CREATE OBJECT r_plane.
  17.   APPEND r_plane TO plane_list.
  18.   r_plane->set_attributes( im_name = 'US Hercules'
  19.   im_planetype = '747-500' ).
  20.   LOOP AT plane_list INTO r_plane.
  21.     r_plane->display_attributes( ).
  22.   ENDLOOP.
  23. * long syntax for functional call:
  24. * CALL METHOD lcl_airplane=>get_n_o_airplanes
  25. * RECEIVING
  26. * re_count = count.
  27. * a little bit shorter:
  28. * lcl_airplane=>get_n_o_airplanes( RECEIVING re_count = count ).
  29. * the shortest syntax for functional call:
  30.   count = lcl_airplane=>get_n_o_airplanes( ).
  31.   WRITE: / 'Total number of planes', count.

  32.   lcl_airplane=>display_n_o_airplanes( ).
复制代码
  1. *&---------------------------------------------------------------------*
  2. *&  包含                YLEON_001_AIRS_A
  3. *&---------------------------------------------------------------------*
  4. CLASS lcl_airplane DEFINITION.
  5.   PUBLIC SECTION.
  6.     CONSTANTS: pos_1 TYPE i VALUE 30.
  7.     METHODS: set_attributes IMPORTING
  8.                               im_name      TYPE string
  9.                               im_planetype TYPE saplane-planetype,
  10.       display_attributes.
  11.     CLASS-METHODS: display_n_o_airplanes,
  12.       get_n_o_airplanes RETURNING VALUE(re_count) TYPE i.

  13.   PRIVATE SECTION.
  14.     DATA: name      TYPE string,
  15.           planetype TYPE saplane-planetype.
  16.     CLASS-DATA: n_o_airplanes TYPE i.

  17. ENDCLASS. "lcl_airplane DEFINITION

  18. CLASS lcl_airplane IMPLEMENTATION.
  19.   METHOD set_attributes.
  20.     name = im_name.
  21.     planetype = im_planetype.
  22. * doesn't make sense so much -
  23. * only in order to get an effect
  24. * after calling display_n_o_airplanes:
  25.     n_o_airplanes = n_o_airplanes + 1.
  26.   ENDMETHOD. "set_attributes

  27.   METHOD display_attributes.
  28.     WRITE: / icon_ws_plane AS ICON,
  29.     / 'Name of airplane:'(001), AT pos_1 name,
  30.     / 'Airplane type'(002), AT pos_1 planetype.
  31.   ENDMETHOD. "display_attributes

  32.   METHOD display_n_o_airplanes.
  33.     WRITE: /, / 'Total number of planes'(ca1),
  34.     AT pos_1 n_o_airplanes LEFT-JUSTIFIED, /.
  35.   ENDMETHOD. "display_n_o_airplanes

  36.   METHOD get_n_o_airplanes.
  37.     re_count = n_o_airplanes.
  38.   ENDMETHOD. "get_n_o_airplanes
  39. ENDCLASS. "lcl_airplane IMPLEMENTATION
复制代码

测试结果:
方法调用.jpg
回复

使用道具 举报

快速回帖

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

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