10-30-2007 10:58 AM
Hi ,
I came across the statement in the code which i am reviewing .
its :
CALL METHOD me->get_id
EXPORTING
internal = internal.
can any one explain me here the use of ' <b>me->get_id'</b>. ?
can i replace above statement by the following given code :
data : cl_lcl type ref to cl_click .
create object cl_lcl.
CALL METHOD cl_lcl->get_id
EXPORTING
internal = internal.
if not , why ??
Thanks ,
Abhishek .
10-30-2007 1:21 PM
Hi Abhishek,
This statement is Often used to differentiate from a subclass attributes / methods to superclass attributes / methods or just differentiate from a global data of the class to a local data to a class.
<b>SAP Documentation Reference</b>
During object creation, me also points to the instance of the generated subclass during the execution of an instance constructor of a superclass that has been called using super->constructor. In the instance constructor of the superclass, or in methods that have been called by the instance constructor, specifying me-> with the method call has no effect. Instead, the method implementations of the superclass are always called.
I think you'll understand if look at the follow example.
REPORT zself_reference.
*----------------------------------------------------------------------*
* CLASS test DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS one DEFINITION.
PUBLIC SECTION.
METHODS get_id EXPORTING global_internal TYPE char01
local_internal TYPE char01.
METHODS test.
DATA v_internal TYPE char01.
ENDCLASS. "one DEFINITION
*----------------------------------------------------------------------*
* CLASS one IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS one IMPLEMENTATION.
METHOD get_id.
DATA v_internal TYPE char01.
* Using ME you can diferentiate from Global, Local and Parameter
* If you need to access Global v_internal definited on Public Section
* you must use ME as identifier to actual instance os class test,
* it's becouse there two DATA with the same name 'v_internal",
* Otherwise you don't need to use ME !
* Accessing Global DATA v_internal
me->v_internal = 'A'.
MOVE me->v_internal TO global_internal.
* If you need to access Local v_internal definited in the method
* you can't use ME !
* Accessing Global DATA v_internal
v_internal = 'B'.
MOVE v_internal TO local_internal.
ENDMETHOD. "get_id
METHOD test.
DATA v_global_internal TYPE char01.
DATA v_local_internal TYPE char01.
* This statement call the method of own class ONE
CALL METHOD get_id
IMPORTING
global_internal = v_global_internal
local_internal = v_local_internal.
WRITE: / 'global_internal of Class One => ', v_global_internal.
WRITE: / 'local_internal of Class One => ', v_local_internal.
ENDMETHOD. "test
ENDCLASS. "one IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS two DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS two DEFINITION INHERITING FROM one.
PUBLIC SECTION.
METHODS get_id REDEFINITION.
METHODS test REDEFINITION.
ENDCLASS. "two DEFINITION
*----------------------------------------------------------------------*
* CLASS two IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS two IMPLEMENTATION.
METHOD get_id.
DATA v_global_internal TYPE char01.
DATA v_local_internal TYPE char01.
* Using the super-reference 'SUPER' you can diferentiate from Global, Local
* and Parameter from Super class and Sub class Class
* This statement call the method of super class ONE
CALL METHOD super->get_id
IMPORTING
global_internal = v_global_internal
local_internal = v_local_internal.
WRITE: / 'global_internal of Super Class One in Class Two => ', v_global_internal.
WRITE: / 'local_internal of Super Class One in Class Two => ', v_local_internal.
DATA v_internal TYPE char01.
* Using the self-reference 'ME' you can diferentiate from Global, Local and Parameter
* If you need to access Global v_internal definited on Public Section
* you must use ME as identifier to actual instance os class test,
* it's becouse there two DATA with the same name 'v_internal",
* Otherwise you don't need to use ME !
* Accessing Global DATA v_internal
me->v_internal = 'C'.
MOVE me->v_internal TO global_internal.
* If you need to access Local v_internal definited in the method
* you can't use ME !
* Accessing Global DATA v_internal
v_internal = 'D'.
MOVE v_internal TO local_internal.
ENDMETHOD. "get_id
METHOD test.
DATA v_global_internal TYPE char01.
DATA v_local_internal TYPE char01.
* This statement call the method of own class ONE
CALL METHOD get_id
IMPORTING
global_internal = v_global_internal
local_internal = v_local_internal.
WRITE: / 'global_internal of Class Two Without ME => ', v_global_internal.
WRITE: / 'local_internal of Class Two Without ME => ', v_local_internal.
* The same Behavior we have with the self-reference
CALL METHOD me->get_id
IMPORTING
global_internal = v_global_internal
local_internal = v_local_internal.
WRITE: / 'global_internal of Class Two With ME => ', v_global_internal.
WRITE: / 'local_internal of Class Two With ME => ', v_local_internal.
ENDMETHOD. "test
ENDCLASS. "two IMPLEMENTATION
DATA o_two TYPE REF TO two.
DATA v_global_internal TYPE char01.
DATA v_local_internal TYPE char01.
START-OF-SELECTION.
CREATE OBJECT o_two.
CALL METHOD o_two->test( ).
Regards.
Marcelo Ramos
10-30-2007 1:21 PM
Hi Abhishek,
This statement is Often used to differentiate from a subclass attributes / methods to superclass attributes / methods or just differentiate from a global data of the class to a local data to a class.
<b>SAP Documentation Reference</b>
During object creation, me also points to the instance of the generated subclass during the execution of an instance constructor of a superclass that has been called using super->constructor. In the instance constructor of the superclass, or in methods that have been called by the instance constructor, specifying me-> with the method call has no effect. Instead, the method implementations of the superclass are always called.
I think you'll understand if look at the follow example.
REPORT zself_reference.
*----------------------------------------------------------------------*
* CLASS test DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS one DEFINITION.
PUBLIC SECTION.
METHODS get_id EXPORTING global_internal TYPE char01
local_internal TYPE char01.
METHODS test.
DATA v_internal TYPE char01.
ENDCLASS. "one DEFINITION
*----------------------------------------------------------------------*
* CLASS one IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS one IMPLEMENTATION.
METHOD get_id.
DATA v_internal TYPE char01.
* Using ME you can diferentiate from Global, Local and Parameter
* If you need to access Global v_internal definited on Public Section
* you must use ME as identifier to actual instance os class test,
* it's becouse there two DATA with the same name 'v_internal",
* Otherwise you don't need to use ME !
* Accessing Global DATA v_internal
me->v_internal = 'A'.
MOVE me->v_internal TO global_internal.
* If you need to access Local v_internal definited in the method
* you can't use ME !
* Accessing Global DATA v_internal
v_internal = 'B'.
MOVE v_internal TO local_internal.
ENDMETHOD. "get_id
METHOD test.
DATA v_global_internal TYPE char01.
DATA v_local_internal TYPE char01.
* This statement call the method of own class ONE
CALL METHOD get_id
IMPORTING
global_internal = v_global_internal
local_internal = v_local_internal.
WRITE: / 'global_internal of Class One => ', v_global_internal.
WRITE: / 'local_internal of Class One => ', v_local_internal.
ENDMETHOD. "test
ENDCLASS. "one IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS two DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS two DEFINITION INHERITING FROM one.
PUBLIC SECTION.
METHODS get_id REDEFINITION.
METHODS test REDEFINITION.
ENDCLASS. "two DEFINITION
*----------------------------------------------------------------------*
* CLASS two IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS two IMPLEMENTATION.
METHOD get_id.
DATA v_global_internal TYPE char01.
DATA v_local_internal TYPE char01.
* Using the super-reference 'SUPER' you can diferentiate from Global, Local
* and Parameter from Super class and Sub class Class
* This statement call the method of super class ONE
CALL METHOD super->get_id
IMPORTING
global_internal = v_global_internal
local_internal = v_local_internal.
WRITE: / 'global_internal of Super Class One in Class Two => ', v_global_internal.
WRITE: / 'local_internal of Super Class One in Class Two => ', v_local_internal.
DATA v_internal TYPE char01.
* Using the self-reference 'ME' you can diferentiate from Global, Local and Parameter
* If you need to access Global v_internal definited on Public Section
* you must use ME as identifier to actual instance os class test,
* it's becouse there two DATA with the same name 'v_internal",
* Otherwise you don't need to use ME !
* Accessing Global DATA v_internal
me->v_internal = 'C'.
MOVE me->v_internal TO global_internal.
* If you need to access Local v_internal definited in the method
* you can't use ME !
* Accessing Global DATA v_internal
v_internal = 'D'.
MOVE v_internal TO local_internal.
ENDMETHOD. "get_id
METHOD test.
DATA v_global_internal TYPE char01.
DATA v_local_internal TYPE char01.
* This statement call the method of own class ONE
CALL METHOD get_id
IMPORTING
global_internal = v_global_internal
local_internal = v_local_internal.
WRITE: / 'global_internal of Class Two Without ME => ', v_global_internal.
WRITE: / 'local_internal of Class Two Without ME => ', v_local_internal.
* The same Behavior we have with the self-reference
CALL METHOD me->get_id
IMPORTING
global_internal = v_global_internal
local_internal = v_local_internal.
WRITE: / 'global_internal of Class Two With ME => ', v_global_internal.
WRITE: / 'local_internal of Class Two With ME => ', v_local_internal.
ENDMETHOD. "test
ENDCLASS. "two IMPLEMENTATION
DATA o_two TYPE REF TO two.
DATA v_global_internal TYPE char01.
DATA v_local_internal TYPE char01.
START-OF-SELECTION.
CREATE OBJECT o_two.
CALL METHOD o_two->test( ).
Regards.
Marcelo Ramos
10-31-2007 5:09 AM
Thanks Marcelo , the explanation and the examples were perfect !!
10-30-2007 2:24 PM
Hi Abhishek,
<b>me-></b> is to refer the class. A class can refer itself using the <b>me-></b>
You cannot replace the ME-> reference with the object name.
Below is an example which would clarifies your doubt.
REPORT Y_TEMP_OOABAP.
CLASS C1 DEFINITION.
PUBLIC SECTION.
DATA: NUM1 TYPE I VALUE 5.
METHODS: TESTMETHOD.
ENDCLASS.
CLASS C1 IMPLEMENTATION.
METHOD TESTMETHOD.
DATA: NUM1 TYPE I VALUE 10.
WRITE:/ ME->NUM1, " Accessing the variable which belongs to a class
NUM1. " Accessing local variable
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA: OREF TYPE REF TO C1.
CREATE OBJECT OREF.
OREF->TESTMETHOD( ).
WRITE:/ OREF->NUM1.
Thanks,
Sreekanth
<i>Reward if it helps you... ;)</i>