是否可以在aapap OO中实现REJECT?

时间:2019-05-20 21:01:50

标签: sap abap

是否可以实施以下想法?这个想法是,它几乎像REJECT语句中的GET一样工作,但是在OO范式内...

start-of-selection

    lv_max_lines = class1->get_max_lines( ).

    do lv_max_lines.
        class2->method1( class1->get_line_by_index( sy-index ) ).
    enddo.


class 2 implementation.

    method method1.
        method2( is_line ).
    endmethod.

    method method2.
        method3( is_line ).
    endmethod.

    method method3.
        if ls_line <> what_I_need.
            class1->reject( ). "or
            class1->reject( is_line ).
            "go back straight to start of selection and execute next iteration,
            "ignoring the rest of method3 and metho2 and method1 from class2.
        endif.
        "more process
    endmethod.

endclass.

当然可以在class2方法的'return语句中使用多个条件来完成此操作,但是其想法是模拟拒绝,它不需要class2进行任何修改,整个工作将是留给class1处理。

我的一个想法是从正在访问的class1表中删除当前行,该行无法按预期工作,实际上,我不确定这将如何工作。

我认为这是不可能实现的,但是无论如何我都想尝试一下。

1 个答案:

答案 0 :(得分:3)

是的,可以通过class-based exceptions从类CX_NO_CHECK继承。

" inherit cx_no_check to let the exception bubble upwards
" without having to redeclare it
class bad_input definition inheriting from cx_no_check.
endclass.

start-of-selection

  lv_max_lines = class1->get_max_lines( ).

  do lv_max_lines times.
    try.      
        class2->method1( class1->get_line_by_index( sy-index ) ).
      catch bad_input.
        " do nothing, just continue with the next line
    endtry.
  enddo.

class class2 definition.
  public section.
    methods method1 importing is_line type whatever.
  private section.
    methods method2 importing is_line type whatever.
    methods method3
      importing
        is_line type whatever.
endclass.

class class2 implementation.

  method method1.
    method2( is_line ).
  endmethod.

  method method2.
    method3( is_line ).
  endmethod.

  method method3.
    if ls_line <> what_I_need.
      raise exception new bad_input( ).
    endif.
    "more process
  endmethod.

endclass.