Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

Here is a simple program that demonstrates the usage of methods, statis and instance constructors  in abap -objects.

Create pf-status with three push buttons 'TEAM_GREEN' 'TEAM_RED' 'TEAM_BLUE' .

Error rendering macro 'code': Invalid value specified for parameter 'lang'
REPORT  z_ooabap_methods.
SELECTION-SCREEN BEGIN OF: SCREEN 100 TITLE title1,LINE.
PARAMETERS: members TYPE i DEFAULT 3.  " No.of members in a team
SELECTION-SCREEN END OF : LINE, SCREEN 100.
**********************************************************************
CLASS: lcl_biker DEFINITION DEFERRED.
**********************************************************************
CLASS lcl_team DEFINITION.
  PUBLIC SECTION.
    TYPES:
          biker_ref TYPE REF TO lcl_biker,
          biker_ref_tab TYPE STANDARD TABLE OF biker_ref,
          BEGIN OF type_s_status,
            text1(10) TYPE c,
            id TYPE i,
            text2(10) TYPE c,
            text3(10) TYPE c,
            gear TYPE i,
            text4(10) TYPE c,
            speed TYPE i,
          END OF type_s_status.
    CLASS-METHODS: class_constructor.
    METHODS: constructor,
             create_team,
             display_list.
  PRIVATE SECTION.
    CLASS-DATA:  counter TYPE i.
    DATA: id TYPE i,
          t_biker TYPE biker_ref_tab,
          fs_biker LIKE LINE OF t_biker.
    DATA: fs_status TYPE type_s_status,
          t_status LIKE SORTED TABLE OF fs_status WITH UNIQUE KEY id.
    METHODS write_list.
ENDCLASS.                    "lcl_team DEFINITION
**********************************************************************
CLASS lcl_biker DEFINITION.
  PUBLIC SECTION.
    METHODS: constructor
               IMPORTING team_id TYPE i
                         members TYPE i,
             biker_status
              EXPORTING line TYPE lcl_team=>type_s_status.
  PRIVATE SECTION.
    CLASS-DATA: counter TYPE i.
*    gear_status TYPE i VALUE 1,
*          speed_status TYPE i.
    DATA: id TYPE i,
          gear_status TYPE i VALUE 1,
          speed_status TYPE i.
ENDCLASS.                    "lcl_biker DEFINITION
**********************************************************************
CLASS lcl_team IMPLEMENTATION.
*-----------------------------------------------------------------------
*The system calls the static constructor once for each class, before the
* class is accessed for the first time. The static constructor cannot
*therefore access the components of its own class.
*-----------------------------------------------------------------------
  METHOD class_constructor.
    title1 = 'Team Members?'.
    CALL SELECTION-SCREEN 100 STARTING AT 5 4.
  ENDMETHOD.                    "class_constructor
*to set team id.
  METHOD constructor.
    counter = counter + 1.
    id = counter.
  ENDMETHOD.                    "constructor
  METHOD create_team.
    DO members TIMES.
      CREATE OBJECT fs_biker
          EXPORTING team_id = id
                    members = members.
      APPEND fs_biker TO t_biker.
      CALL METHOD fs_biker->biker_status
        IMPORTING
          line = fs_status.
      APPEND fs_status TO t_status.
    ENDDO.
  ENDMETHOD.                    "create_team
  METHOD write_list.
    SET TITLEBAR 'TIT1'.
    sy-lsind = 0.
    SKIP TO LINE 1.
    POSITION 1.
    LOOP AT t_status INTO fs_status.
      WRITE:/
               fs_status-text1,
               fs_status-id,
               fs_status-text2,
               fs_status-text3,
               fs_status-gear,
               fs_status-text4,
               fs_status-speed.
    ENDLOOP.
  ENDMETHOD.                    "write_list
  METHOD display_list.
    CALL METHOD write_list.
  ENDMETHOD.                    "display_list
ENDCLASS.                    "lcl_team IMPLEMENTATION
**********************************************************************
CLASS lcl_biker IMPLEMENTATION.
*to set biker id.
  METHOD constructor.
    ADD 1 TO counter.
    id = counter - members * ( team_id - 1 ).
  ENDMETHOD.                    "constructor
  METHOD  biker_status.
    IF gear_status LT 4.
      ADD 1 TO gear_status.
    ENDIF.
    IF speed_status LT 60.
      ADD 10 TO speed_status.
    ENDIF.
    line-text1 = 'Biker'.
    line-id    = id.
    line-text2 = 'Status:'.
    line-text3 = 'Gear'.
    line-gear  = gear_status.
    line-text4  = 'Speed'.
    line-speed = speed_status.
  ENDMETHOD.                    "biker_status
ENDCLASS.                    "lcl_biker IMPLEMENTATION
*******************************************************************
* Global Program Data
*******************************************************************
TYPES: team TYPE REF TO lcl_team.
DATA: team_blue TYPE team,
      team_red TYPE team,
      team_green TYPE team.
DATA color(5) TYPE c.
*******************************************************************
* START-OF-SELECTION.
*******************************************************************
START-OF-SELECTION.
*'create object' invokes the constructor.
  CREATE OBJECT: team_blue,
                 team_red,
                 team_green.
  CALL METHOD : team_blue->create_team,
                team_red->create_team,
                team_green->create_team.
  SET PF-STATUS 'TEAM'.
  WRITE 'SELECT A TEAM !!'.
*******************************************************************
*  AT USER-COMMAND
*******************************************************************
AT USER-COMMAND.
  CASE sy-ucomm.
    WHEN 'TEAM_RED'.
      FORMAT COLOR = 6 INTENSIFIED ON INVERSE ON.
      CALL METHOD team_red->display_list.
    WHEN 'TEAM_GREEN'.
      FORMAT COLOR = 5 INTENSIFIED ON INVERSE ON.
      CALL METHOD team_green->display_list.
    WHEN 'TEAM_BLUE'.
      FORMAT COLOR = 1 INTENSIFIED ON INVERSE ON.
      CALL METHOD team_green->display_list.
    WHEN 'EXIT'.
      LEAVE PROGRAM.
  ENDCASE.

 

Output: 


Execute



 

Displays the Team members of team selected in Green color.


 Upon selecting Team_Red dislays the team members in Red color.



1 Comment

  1. There are many Sample Programs available on the same topic in R/3 System.

    Thanks,

    Vijay