*********** CLASS CONSTRUCTOR***************
CLASS test_11 DEFINITION.
PUBLIC SECTION.
CLASS-METHODS : class_constructor.
METHODS : constructor IMPORTING var TYPE i,
display.
ENDCLASS. "TEST_11 DEFINITION
CLASS test_11 IMPLEMENTATION.
METHOD constructor .
WRITE :/ 'VAR = ', var.
WRITE :/ 'CONST METHOD IS AUTOM CALLED WHEN OBJECT OF CLASS IS CREATED'.
ENDMETHOD. "CONSTRUCTOR
METHOD display.
WRITE :/ 'DISP METHOD IS CALLED EXPLICITLY'.
ENDMETHOD. "DISPLAY
METHOD class_constructor.
WRITE :/ 'CLASS CONST IS EXECUTED ONLE ONCE PER CLASS'.
ENDMETHOD. "CLASS_CONSTRUCTOR
ENDCLASS. "TEST_11 IMPLEMENTATION
START-OF-SELECTION.
DATA : obj1 TYPE REF TO test_11.
DATA : obj2 TYPE REF TO test_11.
CREATE OBJECT obj1 EXPORTING var = 10.
ULINE.
CREATE OBJECT obj2 EXPORTING var = 20.
ULINE.
CALL METHOD obj1->display.
-------------------------------------------------------------------
*******CLASS EVENTS*********
CLASS test_12 DEFINITION.
PUBLIC SECTION.
DATA : num TYPE i VALUE 0.
METHODS : display.
EVENTS : overflow EXPORTING value(var) TYPE i.
ENDCLASS. "TEST_12 DEFINITION
CLASS test_12 IMPLEMENTATION.
METHOD display.
num = num + 1.
IF num > 5.
RAISE EVENT overflow EXPORTING var = num.
ENDIF.
WRITE :/ ' NUM = ', num.
ENDMETHOD. "DISPLAY
ENDCLASS. "TEST_12 IMPLEMENTATION
CLASS event1 DEFINITION.
PUBLIC SECTION.
METHODS handle_over FOR EVENT overflow OF test_12 IMPORTING var .
ENDCLASS. "EVENT1 DEFINITION
CLASS event1 IMPLEMENTATION.
METHOD handle_over.
ULINE.
WRITE :/ ' ENEVT HANDLER METHOD IS CALLED FOR THE EVENT OVERFLOW'.
WRITE :/ ' EXPORTING PARAMETER FORM EVENT TO HANDLER VAR = ', var.
ULINE.
ENDMETHOD. "HANDLE_OVER
ENDCLASS. "EVENT1 IMPLEMENTATION
START-OF-SELECTION.
DATA obj TYPE REF TO test_12.
DATA obj1 TYPE REF TO test_12.
CREATE OBJECT obj.
CREATE OBJECT obj1.
DATA : o_eve TYPE REF TO event1.
CREATE OBJECT o_eve.
SET HANDLER o_eve->handle_over FOR ALL INSTANCES.
DO 10 TIMES.
CALL METHOD obj->display.
ENDDO.
ULINE.
DO 10 TIMES.
CALL METHOD obj1->display.
ENDDO.
-------------------------------------------------------------------------------
***********INHERITANCE(1)************
CLASS test_100 DEFINITION.
PUBLIC SECTION.
DATA : num TYPE i VALUE 10.
METHODS : display,
constructor.
protected section.
data : num2 type i value 20.
methods : calculate.
private section.
data : num3 type i value 30.
methods : calc .
ENDCLASS. "test_100 DEFINITION
CLASS test_100 IMPLEMENTATION.
METHOD display.
WRITE :/ ' value of num in super class = ', num.
calculate( ).
calc( ).
ENDMETHOD. "display
method calculate.
write :/ 'value of protected data object num2 = ', num2.
endmethod.
method calc.
write :/ 'value of private data object num3 = ', num3.
endmethod.
method constructor.
write :/ 'constructor of the super class'.
endmethod.
ENDCLASS. "test_100 IMPLEMENTATION
CLASS test_200 DEFINITION INHERITING FROM test_100.
PUBLIC SECTION.
METHODS : show,
constructor,
display redefinition.
ENDCLASS. "test_200 DEFINITION
CLASS test_200 IMPLEMENTATION.
METHOD show.
WRITE :/ 'method show of subclass'.
calculate( ).
ENDMETHOD. "show
METHOD display.
WRITE :/ 'super class method implemented in sub class'.
ENDMETHOD. "display
method constructor.
call method super->constructor.
write :/ 'constructor of the sub class'.
endmethod.
ENDCLASS. "test_200 IMPLEMENTATION
START-OF-SELECTION.
DATA obj1 TYPE REF TO test_100.
DATA obj2 TYPE REF TO test_200.
CREATE OBJECT : obj1.
uline.
create object obj2.
CALL METHOD obj1->display.
ULINE.
CALL METHOD obj2->show.
ULINE.
CALL METHOD obj2->display.
uline.
---------------------------------------------------------------------------------
*********INHERITANCE(2)******************
class lcl_a definition.
public section.
methods : display_a.
endclass.
class lcl_a implementation.
method display_a.
write :/ 'method of super class'.
endmethod.
endclass.
class lcl_b definition inheriting from lcl_a.
public section.
methods : display_b.
endclass.
class lcl_b implementation.
method display_b.
write :/ 'method of super and sub class'.
endmethod.
endclass.
class lcl_c definition inheriting from lcl_b.
public section.
methods : display_c.
endclass.
class lcl_c implementation.
method display_c.
write :/ 'method of sub class'.
endmethod.
endclass.
-------------------------------------------------------------------------------
*********INHERITANCE(3)******************
class lcl_a definition.
public section.
methods disp.
endclass.
class lcl_a implementation.
method disp.
write :/ 'display method of super class'.
endmethod.
endclass.
class lcl_b definition inheriting from lcl_a.
public section.
methods show.
methods : disp redefinition.
endclass.
class lcl_b implementation.
method show.
write :/ 'show method of sub class'.
endmethod.
method disp.
write :/ 'disp method of super class is redefined in sub class'.
endmethod.
endclass.
start-of-selection.
data : obj1 type ref to lcl_a,
obj2 type ref to lcl_b.
create object obj1.
create object obj2.
call method obj1->disp.
uline.
call method obj2->show.
call method obj2->disp.
-----------------------------------------------------------------------------
*********INHERITANCE(4)******************
CLASS test_100 DEFINITION.
PUBLIC SECTION.
DATA : num TYPE i VALUE 10.
METHODS : display.
ENDCLASS. "test_100 DEFINITION
class test_100 implementation.
method display.
write :/ ' value of num in super class = ', num.
endmethod.
endclass.
class test_200 definition inheriting from test_100.
public section.
methods : show.
endclass.
class test_200 implementation.
method show.
write :/ 'method show of subclass'.
endmethod.
endclass.
start-of-selection.
data obj1 type ref to test_100.
data obj2 type ref to test_200.
create object : obj1, obj2.
call method obj1->display.
uline.
call method obj2->show.
uline.
call method obj2->display.
-------------------------------------------------------------------------------
*********INHERITANCE(5)******************
CLASS test_100 DEFINITION.
PUBLIC SECTION.
DATA : num TYPE i VALUE 10.
METHODS : display.
ENDCLASS. "test_100 DEFINITION
CLASS test_100 IMPLEMENTATION.
METHOD display.
WRITE :/ ' value of num in super class = ', num.
ENDMETHOD. "display
ENDCLASS. "test_100 IMPLEMENTATION
CLASS test_200 DEFINITION INHERITING FROM test_100.
PUBLIC SECTION.
METHODS : show,
display redefinition.
ENDCLASS. "test_200 DEFINITION
CLASS test_200 IMPLEMENTATION.
METHOD show.
WRITE :/ 'method show of subclass'.
ENDMETHOD. "show
METHOD display.
WRITE :/ 'super class method implemented in sub class'.
ENDMETHOD. "display
ENDCLASS. "test_200 IMPLEMENTATION
START-OF-SELECTION.
DATA obj1 TYPE REF TO test_100.
DATA obj2 TYPE REF TO test_200.
CREATE OBJECT : obj1, obj2.
CALL METHOD obj1->display.
ULINE.
CALL METHOD obj2->show.
ULINE.
CALL METHOD obj2->display.
-----------------------------------------------------------------------------
*********INHERITANCE(6)******************
CLASS lcl_a DEFINITION .
PUBLIC SECTION.
METHODS : display final.
ENDCLASS. "lcl_a DEFINITION
CLASS lcl_a IMPLEMENTATION.
method display.
write :/ 'display method called'.
endmethod.
ENDCLASS. "lcl_a IMPLEMENTATION
class lcl_b definition inheriting from lcl_a.
public section.
methods : show.
* methods : display redefinition. " YOU CANT REDEFINE A FINAL METHOD
endclass.
class lcl_b implementation.
method show.
write :/ 'show method called'.
endmethod.
endclass.
start-of-selection..
data obj1 type ref to lcl_a.
data obj2 type ref to lcl_b.
create object obj1.
create object obj2.
obj1->display( ).
obj2->show( ).
obj2->display( ).
------------------------------------------------------------------------------
*********INHERITANCE(7) WITH ABSTRACT CLASS******************
class lcl_a definition abstract.
public section.
methods : display abstract.
class-methods : calc.
endclass.
class lcl_a implementation.
method calc.
write :/ 'non abstract method of abstract class'.
endmethod.
endclass.
class lcl_b definition inheriting from lcl_a.
public section.
methods : show,
display redefinition.
endclass.
class lcl_b implementation.
method show.
write :/ 'method show of sub class'.
endmethod.
method display.
write :/ 'display method of abstract class'.
endmethod.
endclass.
start-of-selection.
data obj1 type ref to lcl_a.
data obj type ref to lcl_b.
create object obj.
call method obj->show.
call method obj->display.
call method lcl_a=>calc.
-----------------------------------------------------------------------------
***********FRIEND CLASS *****************
CLASS DEMO1 DEFINITION DEFERRED. " forward declaration
CLASS DEMO2 DEFINITION FRIENDS DEMO1.
PUBLIC SECTION.
DATA : num TYPE i VALUE 10.
METHODS display.
PRIVATE SECTION .
DATA : p_num TYPE i VALUE 1000.
ENDCLASS. "DEFINITION
CLASS DEMO2 IMPLEMENTATION.
METHOD display.
WRITE :/ 'num =',num.
WRITE :/ 'private data object p_num', p_num.
ENDMETHOD. "display
ENDCLASS.
CLASS DEMO1 DEFINITION.
PUBLIC SECTION.
DATA : obj2 TYPE REF TO DEMO2.
METHODS show.
ENDCLASS. " DEFINITION
CLASS DEMO1 IMPLEMENTATION.
METHOD show.
create object obj2.
WRITE :/ obj2->p_num.
ENDMETHOD. "show
ENDCLASS.
START-OF-SELECTION.
DATA obj TYPE REF TO DEMO2.
CREATE OBJECT obj.
DATA obj1 TYPE REF TO DEMO1.
CREATE OBJECT obj1.
CALL METHOD obj->display.
WRITE / obj->num.
ULINE.
CALL METHOD obj1->show.
----------------------------------------------------------------------------
*****************UNIT TEST OF A METHOD ********************
class lcl_demo definition."#EC *
PUBLIC SECTION.
METHODS : CALCULATE IMPORTING NUM1 TYPE I
NUM2 TYPE I
EXPORTING RES TYPE I."#EC *
endclass.
class lcl_demo implementation.
METHOD CALCULATE.
RES = NUM1 + NUM2.
ENDMETHOD.
endclass.
START-OF-SELECTION.
DATA : OBJ TYPE REF TO LCL_DEMO.
DATA : RESULT TYPE I.
DATA : VAL ."#EC NEEDED
DATA : NN."#EC NEEDED
CLASS TEST DEFINITION FOR TESTING."#AU Risk_Level Harmless
PUBLIC SECTION.
METHODS METHOD_TEST FOR TESTING.
ENDCLASS.
CLASS TEST IMPLEMENTATION.
METHOD METHOD_TEST.
CREATE OBJECT OBJ.
OBJ->CALCULATE( EXPORTING NUM1 = 10
NUM2 = 20
IMPORTING RES = RESULT ).
call method cl_aunit_assert=>assert_equals
exporting
exp = '30'
act = RESULT
msg = 'TWO VALUES ARE NOT SAME'
* level = '2'
* tol =
* quit = METHOD
* ignore_hash_sequence = ABAP_FALSE
receiving
assertion_failed = VAL .
ENDMETHOD.
ENDCLASS.
------------------------------------xxxxx-----------------------------------
1 comment:
sir ur programs are very helpful to me thanks alot
Post a Comment