Different ways to call methods in ABAP

前端 未结 3 2122
暖寄归人
暖寄归人 2021-02-14 08:57

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 interna

3条回答
  •  太阳男子
    2021-02-14 08:58

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

提交回复
热议问题