abap method call as parameter in method call

2019-07-13 12:20发布

问题:

i'm new in abap (OO) but developed before in java and wrote a class abap "cl_caretaker" which should handle the operations on database table and the local copy (intern table) of it.

I want to make the following method call:

caretaker->show_table( caretaker->get_users( ) ) .

with:

 caretaker = cl_caretaker=>get_instance( ). "singleton instance

METHODS:

"! get a list of all user which registrated for FCP
"!
"! @parameter rt_users    | users which are registrated for FCP
   get_users
      RETURNING value(rt_users) TYPE itty_users,


 "! shows the content of a table
 "!
 "! @parameter it_table             | the table we want to visualize
   show_table
      IMPORTING
        value(it_table) TYPE ANY TABLE.

if I split the call in two and store the result of get_users in a tmp variable it works.

DATA:
  gt_tmp_users TYPE caretaker->itty_users.

  gt_tmp_users = caretaker->get_users( ).
  caretaker->show_table( gt_tmp_users ).

So my questions are:

1) is a call like: caretaker->show_table( caretaker->get_users( ) ). possible and if how?

2) I also tried to create a generic variable, which stores all kind of tables. Because i don't want to create for each table kind i use a tmp/help variable. But i got the information that only (german: Formalparameter) dummy parameters of method definitions are allowed to of generic type (eg. TYPE any TABLE ).

Here some stuff I already tried:

DATA:
*    tmpanytable TYPE TABLE OF any.
*    tmpAnyTable TYPE any.
    tmpanytable TYPE REF TO data.

 " needed to store a temporal table
 FIELD-SYMBOLS: <tmpanytable> TYPE ANY TABLE.

* ASSIGN caretaker->get_users( ) TO <tmpAnyTable>.
* <tmpAnyTable> = caretaker->get_users( ).
* caretaker->get_users( ).
*caretaker->show_table( <tmpAnyTable> ).
*caretaker->show_table( caretaker->get_users( ) ).

*CALL METHOD: caretaker->show_table( IMPORTING it_table = caretaker->get_users ).
*CALL METHOD: caretaker->show_table( it_table = caretaker->get_users( ) ).

*COMPUTE caretaker->show_table( it_table = caretaker->get_users( ) ).

*ASSIGN caretaker->get_users() ->* to <tmpAnyTable>.
*Caretaker->show_table( <tmpAnyTable> ).

*call METHOD caretaker->show_table
*                Exporting It_table = caretaker->get_users( ).

*  CREATE DATA tmpanytable TYPE STANDARD TABLE OF (dbtab)
*                            WITH NON-UNIQUE DEFAULT KEY.
*  ASSIGN tmpanytable->* TO <tmpanytable>.

*  CREATE DATA tmpanytable TYPE tabkind OF any Table .
*  ASSIGN tmpanytable->* TO <tmpanytable>.

*GET REFERENCE OF caretaker->get_users() INTO tmpAnyTable.
*caretaker->show_table( tmpAnyTable ) .

回答1:

Method chaining is possible, and methods in operand positions are possible as well, but you need at least SAP_ABA 702 for that.

You can use generic types to pass a table around without knowing its type at runtime. However, you can't create a table without knowing its type. Comparing it to OO principles, you can handle references to an abstract superclass and pass them along between components, but you can't instantiate the abstract superclass. The CREATE DATA statement needs a "concrete data type" to work on, not the "abstract super type STANDARD TABLE". The hard part here is deciding who will know about the type and create the data object.

BTW, you may want to take a look at the built-in Object Services - maybe there's no need to reinvent the database access layer wheel yet again.