Object Oriented Classic Program for Single Table :
Class statement defines a class. A class can be defined completely by its definition and implementation. Class definition contains the properties of the class and it contains Public, Protected & Private section. All the attributes and methods will have to be declared under any of its properties. Class implementation contains the implementation of methods declared in the same class. This method contains the general functionality for which the method is build. A class declaration completes when we define and implement it.
Here is a program which shows a list of data from a single table MARA based on the input on Selection Screen. Selection screen contains a select-option and the pattern is block b1. We are displaying 4 fields of MARA like Material, Date, Name & Type.
Structure of internal table and the declaration of internal table are in the Public section of the class so that every attributes and methods can have the access of the table. In the implementation part we selecting fields from MARA and writing the desired output.
We are creating instance under the START-OF-SELECTION and calling the method. Here the object must be reference to the class.
*&---------------------------------------------------------------------*
*& Report ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zsr_test.
TABLES: mara.
INITIALIZATION.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
SELECT-OPTIONS s_matnr FOR mara-matnr OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1.
*----------------------------------------------------------------------*
* CLASS cls1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 DEFINITION.
PUBLIC SECTION.
TYPES: BEGIN OF ty_mara,
matnr TYPE mara-matnr,
ersda TYPE mara-ersda,
ernam TYPE mara-ernam,
mtart TYPE mara-mtart,
END OF ty_mara.
DATA: wa_mara TYPE ty_mara,
it_mara TYPE STANDARD TABLE OF ty_mara.
METHODS: met1.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS. "cls1 DEFINITION
*----------------------------------------------------------------------*
* CLASS cls1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 IMPLEMENTATION.
METHOD met1.
SELECT matnr ersda ernam mtart
FROM mara INTO TABLE it_mara
WHERE matnr IN s_matnr.
IF sy-subrc = 0.
SORT it_mara BY matnr.
LOOP AT it_mara INTO wa_mara.
AT FIRST.
WRITE: /3 'MATERIAL', 20 'DATE', 33 'NAME', 43 'TYPE'.
ULINE.
SKIP.
ENDAT.
WRITE: / wa_mara-matnr,
wa_mara-ersda,
wa_mara-ernam,
wa_mara-mtart.
ENDLOOP.
ELSE.
MESSAGE 'No data found' TYPE 'I'.
ENDIF.
ENDMETHOD. "met1
ENDCLASS. "cls1 IMPLEMENTATION
START-OF-SELECTION.
DATA: obj1 TYPE REF TO cls1.
CREATE OBJECT: obj1.
CALL METHOD: obj1->met1.
Ouput of Selection Screen:
By giving the proper value the List output is:
Here Material No has been selected from 601010007 to 601010050. Since 601010050 is not in the database the last Material no is 601010049.
Here is a program which shows a list of data from a single table MARA based on the input on Selection Screen. Selection screen contains a select-option and the pattern is block b1. We are displaying 4 fields of MARA like Material, Date, Name & Type.
Structure of internal table and the declaration of internal table are in the Public section of the class so that every attributes and methods can have the access of the table. In the implementation part we selecting fields from MARA and writing the desired output.
We are creating instance under the START-OF-SELECTION and calling the method. Here the object must be reference to the class.
*&---------------------------------------------------------------------*
*& Report ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zsr_test.
TABLES: mara.
INITIALIZATION.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
SELECT-OPTIONS s_matnr FOR mara-matnr OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1.
*----------------------------------------------------------------------*
* CLASS cls1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 DEFINITION.
PUBLIC SECTION.
TYPES: BEGIN OF ty_mara,
matnr TYPE mara-matnr,
ersda TYPE mara-ersda,
ernam TYPE mara-ernam,
mtart TYPE mara-mtart,
END OF ty_mara.
DATA: wa_mara TYPE ty_mara,
it_mara TYPE STANDARD TABLE OF ty_mara.
METHODS: met1.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS. "cls1 DEFINITION
*----------------------------------------------------------------------*
* CLASS cls1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 IMPLEMENTATION.
METHOD met1.
SELECT matnr ersda ernam mtart
FROM mara INTO TABLE it_mara
WHERE matnr IN s_matnr.
IF sy-subrc = 0.
SORT it_mara BY matnr.
LOOP AT it_mara INTO wa_mara.
AT FIRST.
WRITE: /3 'MATERIAL', 20 'DATE', 33 'NAME', 43 'TYPE'.
ULINE.
SKIP.
ENDAT.
WRITE: / wa_mara-matnr,
wa_mara-ersda,
wa_mara-ernam,
wa_mara-mtart.
ENDLOOP.
ELSE.
MESSAGE 'No data found' TYPE 'I'.
ENDIF.
ENDMETHOD. "met1
ENDCLASS. "cls1 IMPLEMENTATION
START-OF-SELECTION.
DATA: obj1 TYPE REF TO cls1.
CREATE OBJECT: obj1.
CALL METHOD: obj1->met1.
Ouput of Selection Screen:
By giving the proper value the List output is:
Here Material No has been selected from 601010007 to 601010050. Since 601010050 is not in the database the last Material no is 601010049.
No comments:
Post a Comment