"Statement is not accessible" 声明本地 类 后出错

"Statement is not accessible" error after declaration of local classes

以下 ABAP 程序抛出错误 Statement is not accessible。 该程序包含一个接口,以及两个类:c2继承自c1。 语句 CREATE OBJECT cref1.

中抛出错误
REPORT  z_upcast_downcast_objref.
INTERFACE i1.
  DATA a1 TYPE i.
ENDINTERFACE.                    "i1

INTERFACE i2.
  INTERFACES i1.
  ALIASES a1 FOR i1~a1.
  DATA a2 TYPE i.
ENDINTERFACE.                    "i2

CLASS c1 DEFINITION.
  PUBLIC SECTION.
    INTERFACES i2.
ENDCLASS.                    "c1 DEFINITION

CLASS c2 DEFINITION INHERITING FROM c1.
  PUBLIC SECTION.
    METHODS m1.
ENDCLASS.                    "c2 DEFINITION

CLASS c2 IMPLEMENTATION.
  METHOD m1.
    WRITE : / 'In c2->m1 method'.
  ENDMETHOD.                    "c2
ENDCLASS.                    "c2 DEFINITION

DATA: iref TYPE REF TO i2,
      cref1 TYPE REF TO c1,
      cref2 TYPE REF TO c2.

CREATE OBJECT cref1.

TRY.
    cref2 ?= iref.
    CALL METHOD cref2->('M1').
  CATCH cx_sy_move_cast_error
        cx_sy_dyn_call_illegal_method.
ENDTRY.

编译器通知您它无法执行 CREATE OBJECT 语句,因为它没有分配给隐式 START-OF-SELECTION 块。您在这里有 2 个选项:

  1. 通过在创建对象

    之前放置此行,将其分配给显式 START-OF-SELECTION
    ...
    START-OF-SELECTION.
    
    CREATE OBJECT cref1.
    ...
    
  2. 将您的 c2 class 的实施放在报告末尾。

补充:顺便说一句,你提出了一个我没有意识到的有趣问题。我在 SCN (1,2,3) 上发现了一堆关于这个问题的报告,其中人们提出了正确的解决方案,但没有人能够以基于证据和论证充分的方式自信地解释它。但我终于可以做到了:)
尽管众所周知,所有非声明性语句都分配给隐式 SOS 事件(如果没有声明其他事件),但当我们声明本地 [=39] 时,这 不是重点 =]是的,这里是如何解释的 by SAP:

The position of the implementation part in the source code is thus unimportant. For clarity, however, you should either put all the implementation parts of local classes at the end of the program, or directly after the relevant definition part. If you do the latter, note that you must then assign subsequent non-declarative statements explicitly to a processing block such as START-OF-SELECTION, so that they can be accessed.