10-17-2008 12:27 PM
Hi all,
I have two classes Mother and Child, and I use this code to print the name of the class in use
DATA m TYPE REF TO zmother.
DATA class_name type string VALUE 'CHILD'.
CREATE OBJECT m TYPE (class_name).
CALL METHOD m->PrintName .
So, what is the source code that I have to put in the PrintName methode?
Thanks.
10-17-2008 12:49 PM
you can try like this..
REPORT ztest_method.
*----------------------------------------------------------------------*
* CLASS c1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c1 DEFINITION.
PUBLIC SECTION.
METHODS: get_data.
ENDCLASS. "c1 DEFINITION
*----------------------------------------------------------------------*
* CLASS c1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c1 IMPLEMENTATION.
METHOD get_data.
ENDMETHOD. "get_data
ENDCLASS. "c1 IMPLEMENTATION
DATA oref TYPE REF TO object.
DATA: method TYPE string.
DATA: cla_name(20).
START-OF-SELECTION.
cla_name = 'C1'.
CREATE OBJECT oref TYPE (cla_name).
BREAK-POINT.
CALL METHOD oref->(method).
10-17-2008 12:49 PM
you can try like this..
REPORT ztest_method.
*----------------------------------------------------------------------*
* CLASS c1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c1 DEFINITION.
PUBLIC SECTION.
METHODS: get_data.
ENDCLASS. "c1 DEFINITION
*----------------------------------------------------------------------*
* CLASS c1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c1 IMPLEMENTATION.
METHOD get_data.
ENDMETHOD. "get_data
ENDCLASS. "c1 IMPLEMENTATION
DATA oref TYPE REF TO object.
DATA: method TYPE string.
DATA: cla_name(20).
START-OF-SELECTION.
cla_name = 'C1'.
CREATE OBJECT oref TYPE (cla_name).
BREAK-POINT.
CALL METHOD oref->(method).
10-17-2008 12:56 PM
Ok, but how can I get the name of the object class that calls the methode!
REPORT ztest_method.
*----------------------------------------------------------------------*
* CLASS c1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c1 DEFINITION.
PUBLIC SECTION.
METHODS: get_data.
ENDCLASS. "c1 DEFINITION
*----------------------------------------------------------------------*
* CLASS c1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c1 IMPLEMENTATION.
METHOD get_data.
*****************************************************************************
****** What is the code to put here to know the name of this Class?*
*****************************************************************************
ENDMETHOD. "get_data
ENDCLASS. "c1 IMPLEMENTATION
DATA oref TYPE REF TO object.
DATA: method TYPE string.
DATA: cla_name(20).
START-OF-SELECTION.
cla_name = 'C1'.
CREATE OBJECT oref TYPE (cla_name).
BREAK-POINT.
CALL METHOD oref->(method).
10-17-2008 1:03 PM
10-17-2008 1:05 PM
why do you need to know the object name..? it is completely depends on the class. in the example get_data is used to get the data from some db table.
what exactly you need, can you explain in detail,.
10-17-2008 1:11 PM
I programming a framwork, and I want to know the name the class to call the appropriate Transformation programme.
So I don't want to call the transformations in each Child class, but to call it ones in the Mother class.
10-17-2008 2:04 PM
10-17-2008 3:55 PM
An other question, please. How can I retrieve Information about a class attributs by coding?
And is it possible to access to the attributs of the Child class from the Mother class?
And thinks for the answer
10-17-2008 8:13 PM
Hi,
you can use run-time type identification as in the following example.
REPORT z_demo_classdescr.
*----------------------------------------------------------------------*
* CLASS lcl_myclass DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_myclass DEFINITION.
PUBLIC SECTION.
METHODS main.
ENDCLASS. "lcl_myclass DEFINITION
DATA:
gr_myobj TYPE REF TO lcl_myclass.
START-OF-SELECTION.
CREATE OBJECT gr_myobj.
gr_myobj->main( ).
*----------------------------------------------------------------------*
* CLASS lcl_myclass IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_myclass IMPLEMENTATION.
METHOD main.
DATA:
lv_name TYPE string.
lv_name = cl_abap_classdescr=>get_class_name( me ).
WRITE: / lv_name.
ENDMETHOD. "main
ENDCLASS. "lcl_myclass IMPLEMENTATION
Cheers,
Thorsten