Tuesday, January 03, 2012

OO ABAP - Friendship via subclasses

*&---------------------------------------------------------------------*
*& Report  YSAP_SUBCLASS_FRIENDS
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  YSAP_SUBCLASS_FRIENDS.

tables: pa0002.

parameters: pernr type pa0002-pernr.

databegin of stru,
      pernr type pa0002-pernr,
      vorna type pa0002-vorna,
      nachn type pa0002-nachn,
      begda type pa0002-begda,
      endda type pa0002-endda,
      end of stru.

data: itab like table of stru,
      wa like stru.

class class1 definition.
  PUBLIC SECTION.
  methods: class1_method.
  endclass.

class class2 DEFINITION INHERITING FROM class1.
  PUBLIC SECTION.
  methods: class2_method.
  endclass.

class class3 definition friends class1.
  public SECTION.
  data: string1 type string value 'I am string1 of class3'.
  endclass.

class class1 implementation.
  method class1_method.
  write:/ 'data from pa0002 for', pernr, '. ''from class1.'.
    select * from pa0002
      into CORRESPONDING FIELDS OF TABLE itab
      where pernr eq pernr.
  loop at itab into wa.
    write: /  wa-pernr,
           /  wa-vorna,
           /  wa-nachn,
           /  wa-begda,
           /  wa-endda.
    endloop.
    Skip.
    uline.
    skip.
    data: class3_object type ref to class3.
    create object class3_object.
    write: / class3_object->string1 color 7.
    uline.
    skip.
    endmethod.
endclass.

class class2 implementation.
  method class2_method.
    data: class3_object1 type ref to class3.
    create object class3_object1.
    write:/ 'Accessing string1 from class2' color 4.
    write:/ class3_object1->string1.
    skip.
    uline.
    endmethod.
    endclass.

START-OF-SELECTION.

data: class1_object type ref to class1,
      class2_object type ref to class2.
create object:class1_object, class2_object.
call method: class1_object->class1_method,
             class2_object->class2_method.


 


 

No comments:

Post a Comment