Different ways to call methods in ABAP

2020-07-10 03:40发布

问题:

Sorry for this basic ABAP question. What are the different ways to call methods in ABAP? And what are their "official" names? I've heard of perform, method call, and internal/inline method call.

Perform uses the PERFORM keyword and method call the CALL METHOD syntax, I guess. But what is an "internal" or "inline method call"?

回答1:

These are the possibilities of an inline method call.

If you are calling so called functional method which has only IMPORTING parameters and optionally one RETURN parameter you can call it like this.

CLASS lcl_test DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS:
      func_meth
        IMPORTING
          i_param TYPE i
        RETURNING
          VALUE(r_res) TYPE char1.
ENDCLASS.

l_res = lcl_test=>func_meth( 1 ).

* you could also call it like this
l_res = lcl_test=>func_meth( i_param = 1 ).

* also this variant is possible
l_res = lcl_test=>func_meth( EXPORTING i_param = 1 ).

* the traditional CALL METHOD syntax would be like this
CALL METHOD lcl_test=>func_meth
  EXPORTING
    i_param = 1
  RECEIVING
    r_res = l_res.

If there is more than one IMPORTING parameter you have to specify names of the parameters.

CLASS lcl_test DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS:
      func_meth
        IMPORTING
          i_param1 TYPE i
          i_param2 TYPE i
        RETURNING
          VALUE(r_res) TYPE char1.
ENDCLASS.

l_res = lcl_test=>func_meth(
   i_param1 = 1
   i_param2 = 2
).

If there are EXPORTING or CHANGING parameters in the method then an inline call is still possible but the parameter categories have to be explicitly specified.

CLASS lcl_test DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS:
      func_meth
        IMPORTING
          i_param TYPE i
        EXPORTING
          e_param TYPE c
        CHANGING
          c_param TYPE n.
ENDCLASS.

lcl_test=>func_meth(
  EXPORTING
    i_param = 1
  IMPORTING
    e_param = l_param
  CHANGING
    c_param = l_paramc
).


回答2:

Good luck in your quest - you will find this task to be much harder than anticipated. For example, ABAP contains a macro processing facility that will make it really hard to find out that an actual method call is taking place. A malicious example that will compile nonetheless:

DATA: l_foo TYPE c LENGTH 32.

DEFINE foo.
  l_&4 = cl_&1_&3&5&2&9if_&1_&3&5_&8~&7_&3&5_c&6( ).
END-OF-DEFINITION.

foo system = u foo uid 32 create static >.

You will find that macros are used extensively in some parts of the system. Good luck finding method calls in that kind of stuff without using the built-in parser and macro processor.



回答3:

You have massively mixed up many things. Lets be kind, and start with some basics here:

Method is the official name of a member function of a class, means, if You talk about methods, You talk about object oriented prorgamming ( OOP ). There are about two or three ways of method call variants, at least in abap. Lets go into detail later, after You promise me, to read about this, If You decide to develop following OO-paradigms. And You should.

Function is just a function, a small(or big) piece of code, which can be called from several callers, to be reused. It is no OOP paradigm, and exists since prorgamming started. Nevertheless in abap it has a slight "bigger" definition, let us talk about this in detail, if You promise me, to read about it. And You should, If You want to develop ABAP at all.

Perform ? "Perform" is an abap keyword to call form-routines. Form-routines are the real old school functions-equivalent in abap, most similiar to functions from the good old times, when programming started. Small pieces of reusable code, which can be calles from anywhere in Your prorgamm, but sometimes from somewhere else ( this is too far for You now ).

So, if You are mixing up those three ( or two of those three ) already, then You are not even in a good position, to talk about internal or inline, at all. I am sorry, but it is, as it is. It is no offending.

Do You ever developed in another language ?



标签: call abap