gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:21:04 25 4
gpt4 key购买 nike

以下 ABAP 程序抛出错误 Statement is not accessible .
该程序包含一个接口(interface)和两个类: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 个选项:

  • 将其分配给显式 START-OF-SELECTION通过在创建对象之前放置此行来阻止
    ...
    START-OF-SELECTION.

    CREATE OBJECT cref1.
    ...
  • 您的 c2 的实现上课至报告结束。

  • 加法 : 顺便说一下,你提出了一个我不知道的有趣问题。我在 SCN( 123)上发现了一堆关于这个问题的报告,人们提出了正确的解决方案,但没有人能自信地以循证和充分论证的方式解释它。但最后我可以做到:)
    尽管众所周知的事实是所有非声明性语句都分配给隐式 SOS 事件(如果没有声明其他事件),这是 不是重点当我们声明本地类时,这是如何解释的 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.

    关于class - "Statement is not accessible"本地类声明后出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37226918/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com