将abap方法调用作为方法调用中的参数 [英] abap method call as parameter in method call

查看:726
本文介绍了将abap方法调用作为方法调用中的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是abap(OO)的新手,但之前是用java开发的,并编写了一个abap类"cl_caretaker",该类应处理对数据库表及其本地副本(实习生表)的操作.

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( ) ) .

具有:

 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.

如果我将通话分为两部分,并将get_users的结果存储在tmp变量中,那么它将起作用.

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 ).

所以我的问题是:

1)是这样的通话:caretaker->show_table( caretaker->get_users( ) ). 可能的,以及如何做到的?

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

2)我也尝试创建一个通用变量,该变量存储所有类型的表. 因为我不想为每种表创建,所以我使用了tmp/help变量. 但是我得到的信息是,仅方法定义的(德语:Formalparameter)伪参数才允许使用泛型类型(例如TYPE any TABLE).

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 ) .

推荐答案

方法链接是可能的,并且位于操作数位置的方法也是可能的,但是您为此至少需要SAP_ABA 702.

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

您可以使用泛型类型来传递表,而在运行时不知道其类型.但是,您无法在不知道表类型的情况下创建表.将其与OO原理进行比较,您可以处理对抽象超类的引用,并将其在组件之间传递,但是您不能实例化抽象超类. CREATE DATA语句需要具体数据类型"才能工作,而不是抽象超级类型STANDARD TABLE".这里最难的部分是确定谁将了解类型并创建数据对象.

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.

这篇关于将abap方法调用作为方法调用中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆