方法调用作为另一个方法调用的参数? [英] Method call as a parameter for another method call?

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

问题描述

我是 abap (OO) 的新手,但之前是用 Java 开发的,并编写了一个 abapcl_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.

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

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