10-17-2008 4:33 PM
Hi all,
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?
REPORT ztest_method.
*----------------------------------------------------------------------*
****** CLASS mother DEFINITION
CLASS mother DEFINITION.
PUBLIC SECTION.
METHODS: get_data.
ENDCLASS. "mother DEFINITION
*----------------------------------------------------------------------*
****** CLASS child DEFINITION
CLASS child DEFINITION inheriting from mother.
ENDCLASS. "child DEFINITION
*----------------------------------------------------------------------*
******* CLASS mother IMPLEMENTATION
CLASS mother IMPLEMENTATION.
METHOD get_data.
***Can I access to child class attributs here?
ENDMETHOD. "get_data
ENDCLASS. "mother IMPLEMENTATION
DATA oref TYPE REF TO CHILD.
DATA: method TYPE string.
DATA: cla_name(20).
START-OF-SELECTION.
CREATE OBJECT oref .
CALL METHOD oref->method). "Here I want to access to this class's attribut without redefine it
10-17-2008 4:41 PM
You can not access the attributes of the child class in the Super class.
But, you can use the Attributes of the Child class with the reference of the Super Class if that reference has been initiated using the Child class (Narrow Casting).
For your example, if you create a object for the Child class first and than assign it to Super Class than you can access the attributes or methods of the Child class using the Super Class reference.
Check the example on Narrow Casting: http://help-abap.blogspot.com/2008/09/abap-objects-narrowing-cast-upcasting.html
Regards,
Naimesh Patel
10-17-2008 4:46 PM
Okay, but I don't want to redifine the method in the child class.
I want to impliment the method once in the mother class and not in its childs!
10-17-2008 4:50 PM
You not need to redefine. I have just provided for a example to make a differentiation between Super's method and Child's Method.
Can you share, which requirement leads you think to access the child attribute in the Super method?
Regards,
Naimesh Patel
10-17-2008 5:09 PM
I'm implementing a class that provid the Serializastion from ABAP to XML using XSLT transformation. So I want my child classes use the serialization without implementing it but just by inheriting it from the mother class.
10-17-2008 7:09 PM
Hello Noureddine
For serialization of class instance it is just sufficient to add interface IF_SERIALIZABLE to you class (see blog [ABAP Persistent Classes: Coding without SQL|https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/1013] [original link is broken] [original link is broken] [original link is broken]; by Thomas Jung). There is no kind of implementation of any interface method required.
The interface as public component of your superclass is inherited to its subclasses.
Regards
Uwe
10-20-2008 12:49 PM
So is there any way to get the names of all attributes in a class?
10-28-2008 10:16 PM
Hi,
a very easy way is to define an attribute TYPE REF TO DATA in the super class and write generic access methods which work with it by dereferencing it and assigning a field symbol. This might even save you the effort of creating a class hierarchy, because one class may be able to handle all the data objects whose types are only known at run-time.
In the below example, the only specific thing about the sub class is the constructor in which the data object is created and typed:
REPORT ztesta.
*----------------------------------------------------------------------*
* CLASS lcl_mother DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_mother DEFINITION.
PUBLIC SECTION.
METHODS get_data EXPORTING es_data TYPE any.
METHODS set_data IMPORTING is_data TYPE any.
PROTECTED SECTION.
DATA mr_data TYPE REF TO data.
ENDCLASS. "lcl_mother DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_child1 DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_child1 DEFINITION INHERITING FROM lcl_mother.
PUBLIC SECTION.
METHODS constructor.
ENDCLASS. "lcl_child1 DEFINITION
DATA:
lr_child TYPE REF TO lcl_mother,
ls_but000_in TYPE but000,
ls_but000_out type but000.
START-OF-SELECTION.
CREATE OBJECT lr_child TYPE lcl_child1.
ls_but000_in-partner = '0000004711'.
lr_child->set_data( ls_but000_in ).
lr_child->get_data( IMPORTING es_data = ls_but000_out ).
write: / ls_but000_out-partner.
*----------------------------------------------------------------------*
* CLASS lcl_mother IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_mother IMPLEMENTATION.
METHOD get_data.
FIELD-SYMBOLS:
<fs_data> TYPE ANY.
ASSIGN mr_data->* TO <fs_data>.
MOVE-CORRESPONDING <fs_data> TO es_data.
ENDMETHOD. "get_data
METHOD set_data.
FIELD-SYMBOLS:
<fs_data> TYPE ANY.
ASSIGN mr_data->* TO <fs_data>.
MOVE-CORRESPONDING is_data TO <fs_data>.
ENDMETHOD. "set_data
ENDCLASS. "lcl_mother IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS lcl_child1 IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_child1 IMPLEMENTATION.
METHOD constructor.
super->constructor( ).
CREATE DATA mr_data TYPE but000.
ENDMETHOD. "constructor
ENDCLASS. "lcl_child1 IMPLEMENTATION
Another variant would be to define an attribute MV_DATA which is typed individually in each sub class and program dynamically against it in the super class using field symbols.
*&---------------------------------------------------------------------*
*& Report ZTESTA
*&---------------------------------------------------------------------*
REPORT ztesta.
*----------------------------------------------------------------------*
* CLASS lcl_mother DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_mother DEFINITION.
PUBLIC SECTION.
METHODS get_data EXPORTING es_data TYPE any.
METHODS set_data IMPORTING is_data TYPE any.
ENDCLASS. "lcl_mother DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_child1 DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_child1 DEFINITION INHERITING FROM lcl_mother.
PUBLIC SECTION.
DATA: ms_data TYPE but000.
METHODS constructor.
ENDCLASS. "lcl_child1 DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_child2 DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_child2 DEFINITION INHERITING FROM lcl_mother.
PUBLIC SECTION.
DATA: ms_data TYPE t000.
METHODS constructor.
ENDCLASS. "lcl_child1 DEFINITION
DATA:
lr_child1 TYPE REF TO lcl_mother,
lr_child2 TYPE REF TO lcl_mother,
ls_but000 TYPE but000,
ls_t000 TYPE t000.
START-OF-SELECTION.
CREATE OBJECT lr_child1 TYPE lcl_child1.
lr_child1->get_data( IMPORTING es_data = ls_but000 ).
WRITE: / ls_but000-partner.
CREATE OBJECT lr_child2 TYPE lcl_child2.
lr_child2->get_data( IMPORTING es_data = ls_t000 ).
WRITE: / ls_t000-mwaer.
*----------------------------------------------------------------------*
* CLASS lcl_mother IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_mother IMPLEMENTATION.
METHOD get_data.
FIELD-SYMBOLS:
<fs_data> TYPE ANY.
ASSIGN ('MS_DATA') TO <fs_data>.
es_data = <fs_data>.
ENDMETHOD. "get_data
METHOD set_data.
FIELD-SYMBOLS:
<fs_data> TYPE ANY.
ASSIGN ('MS_DATA') TO <fs_data>.
<fs_data> = is_data.
ENDMETHOD. "set_data
ENDCLASS. "lcl_mother IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS lcl_child1 IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_child1 IMPLEMENTATION.
METHOD constructor.
super->constructor( ).
SELECT SINGLE * FROM but000 INTO ms_data.
ENDMETHOD. "constructor
ENDCLASS. "lcl_child1 IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS lcl_child2 IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_child2 IMPLEMENTATION.
METHOD constructor.
super->constructor( ).
SELECT SINGLE * FROM t000 INTO ms_data.
ENDMETHOD. "constructor
ENDCLASS. "lcl_child2 IMPLEMENTATION
I hope this helps!
Regards,
Thorsten
10-30-2008 9:42 AM
10-30-2008 9:46 AM
10-30-2008 10:50 AM
Hi, sorry . I don't know how to get the, but here is the solution:
DATA: l_typedesc TYPE REF TO cl_abap_typedescr.
data: l_classdesc TYPE REF TO cl_abap_classdescr.
data: l_attribute TYPE abap_attrdescr.
data : att_ref type ref to any.
FIELD-SYMBOLS : <att> TYPE ANY . "abap_attrname.
DATA : name TYPE string.
CLEAR st_root_out.
CALL METHOD cl_abap_classdescr=>describe_by_object_ref
EXPORTING
p_object_ref = me
RECEIVING
p_descr_ref = l_typedesc.
l_classdesc ?= l_typedesc.
LOOP AT l_classdesc->attributes INTO l_attribute "-
WHERE type_kind = cl_abap_classdescr=>typekind_string "-
OR type_kind = cl_abap_classdescr=>typekind_num "-
OR type_kind = cl_abap_classdescr=>typekind_date "-
OR type_kind = cl_abap_classdescr=>typekind_packed "-
OR type_kind = cl_abap_classdescr=>typekind_time "-
OR type_kind = cl_abap_classdescr=>typekind_char "-
OR type_kind = cl_abap_classdescr=>typekind_hex "-
OR type_kind = cl_abap_classdescr=>typekind_float "-
OR type_kind = cl_abap_classdescr=>typekind_int "-
OR type_kind = cl_abap_classdescr=>typekind_table "-
OR type_kind = cl_abap_classdescr=>typekind_struct2 "-
OR type_kind = cl_abap_classdescr=>typekind_struct1 "-
OR type_kind = cl_abap_classdescr=>typekind_oref
OR type_kind = cl_abap_classdescr=>typekind_time. "-
name = l_attribute-name .
ASSIGN me->(name) TO <att>.
CHECK sy-subrc = 0.
GET REFERENCE OF <att> INTO att_ref. " here I get the reference
ENDLOOP.
10-30-2008 10:56 AM