SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Polymophism via inheritance



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ABAP Objects
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1639

PostPosted: Fri Sep 21, 2007 5:04 pm    Post subject: Polymophism via inheritance Reply with quote

Code:
REPORT s_polymophism_via_inheritance.

* Class Declarations

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 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 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 Implementations

CLASS ship IMPLEMENTATION.
  METHOD get_name.
    r_name = name.
  ENDMETHOD.
ENDCLASS.

*

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.
  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.

DATA: name TYPE string,
      range TYPE f.

* Classical Processing Blocks

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.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ABAP Objects All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.