OOPS ABAP: Usual ABAP Class(Local Class Creation using Instance Method)
Usual ABAP Class
Class : Class is a collection of objects(Methods, Attributes, Events, Interfaces)
Object: An instance of class is known as object
Types of Classes:
1.Global Class
The T-code 'SE24' is used to create Global Classes. They can be reusable.
a)Usual ABAP Class --- The purpose of this class is to write the logic.
b)Exception Class --- The purpose of this class is to to raise and handle the exception.
c)Persistence Class --- The purpose of this class is to perform the database operations(Insert,Update,Delete)
d)Unit Test Class --- The purpose of this class is to write unit test cases.
2.Local Class
Local classes are created in program and cannot be reusable.
Class Definition : Declaration part
Class Implementation : Logic
Types of Methods:
a)Instance Method:Instance methods are called by creating object.They are called using (->).
keyword: class.
b)Static Method: Object creation is not needed to call static methods. They are called using(=>).
keyword : class-methods.
Visibility Control :
a)Public : Can be accessed with in the class, with in the sub classes, Outside of the class.
b)Private: Can be accessed with in the class.
c)Protected: Can be accessed with in the class, with in the subclass, not outside the class.
Types of parameters in class methods:
a)Importing -- Input
b)Exporting -- Output
c) Changing -- Input/Output
d)Returning -- A method have any no.of Exporting and Changing parameters, but returning is always one.
Declaration and creation of Object
DATA : LO_OBJECT TYPE REF TO ZCLS_TEST.
CREATE OBJECT LO_OBJECT.
Example on Local Classes:
REPORT zvb_local_cls.
TYPES: BEGIN OF ty_vbak,
vbeln TYPE vbak-vbeln,
erdat TYPE vbak-erdat,
erzet TYPE vbak-erzet,
END OF ty_vbak,
BEGIN OF ty_vbap,
vbeln TYPE vbap-vbeln,
posnr TYPE vbap-posnr,
END OF ty_vbap,
BEGIN OF ty_final,
vbeln TYPE vbak-vbeln,
erdat TYPE vbak-erdat,
erzet TYPE vbak-erzet,
posnr TYPE vbap-posnr,
END OF ty_final.
DATA:it_vbak TYPE TABLE OF ty_vbak,
is_vbak TYPE ty_vbak,
it_vbap TYPE TABLE OF ty_vbap,
is_vbap TYPE ty_vbap,
it_final TYPE TABLE OF ty_final,
is_final TYPE ty_final.
PARAMETERS : p_vbeln TYPE vbak-vbeln.
CLASS class1 DEFINITION.
PUBLIC SECTION.
METHODS display IMPORTING p_vbeln TYPE vbeln_va
EXPORTING it_final TYPE ztt_so.
ENDCLASS.
CLASS class1 IMPLEMENTATION.
METHOD display.
SELECT vbeln erdat erzet
FROM vbak
INTO TABLE it_vbak
WHERE vbeln = p_vbeln.
IF it_vbak IS NOT INITIAL.
SELECT vbeln posnr
FROM vbap
INTO TABLE it_vbap
FOR ALL ENTRIES IN it_vbak
WHERE vbeln = it_vbak-vbeln.
ENDIF.
LOOP AT it_vbap INTO is_vbap.
is_final-vbeln = is_vbap-vbeln.
is_final-posnr = is_vbap-posnr.
READ TABLE it_vbak INTO is_vbak WITH KEY vbeln = is_vbap-vbeln.
is_final-erdat = is_vbak-erdat.
is_final-erzet = is_vbak-erzet.
APPEND is_final TO it_final.
ENDLOOP.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA:lo_object TYPE REF TO class1.
CREATE OBJECT lo_object.
CALL METHOD lo_object->display
EXPORTING
p_vbeln = p_vbeln
IMPORTING
it_final = it_final.
IF it_final IS NOT INITIAL.
LOOP AT it_final INTO is_final.
WRITE: / is_final-vbeln,
10 is_final-posnr,
30 is_final-erdat,
50 is_final-erzet.
ENDLOOP.
ENDIF.
Comments
Post a Comment