二维码

[面向对象] ABAP面向对象中类的层次结构

Twilight发表于 2016-04-15 10:15Twilight 最后回复于 2016-04-15 10:15 [复制链接] 3485 0

在一个面向对象数据库模式中,会出现许多个相似但又有所不同的类。

例如,教员职工和学生两个类都有身份证号,姓名,年龄,性别,住址等属性,也有些相同的方法。当然,教职工和学生对象中有一些特殊的属性和方法。教职员工和学生的公共属性和方法部分可以统一定义,各自的特殊属性和方法部分可以分别定义。为此,面向对象数据结构给出了类层次结构!

下面给出一个案例:
  1. REPORT yleon_002.
  2. TYPE-POOLS icon.

  3. INCLUDE yleon_002_a.

  4. DATA: r_plane     TYPE REF TO lcl_airplane,
  5.       r_cargo     TYPE REF TO lcl_cargo_plane,
  6.       r_passenger TYPE REF TO lcl_passenger_plane,
  7.       plane_list  TYPE TABLE OF REF TO lcl_airplane.

  8. START-OF-SELECTION.

  9.   lcl_airplane=>display_n_o_airplanes( ).
  10.   CREATE OBJECT r_passenger
  11.     EXPORTING
  12.       im_name      = 'LH BERLIN'
  13.       im_planetype = '747-400'
  14.       im_seats     = 345.
  15.   CREATE OBJECT r_cargo
  16.     EXPORTING
  17.       im_name      = 'US Hercules'
  18.       im_planetype = '747-500'
  19.       im_cargo     = 533.
  20.   r_cargo->display_attributes( ).
  21.   r_passenger->display_attributes( ).
  22.   lcl_airplane=>display_n_o_airplanes( ).
复制代码
  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
复制代码

测试结果:
类层次结构.jpg
回复

使用道具 举报

快速回帖

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

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