[英]How can I define a filled structure/table as class constant in ABAP Objects
I want to have an immutable predefined table as class variable. 我想要一个不变的预定义表作为类变量。 How do I define such a variable? 如何定义这样的变量?
This is an old question, with a simple answer: Just create a static method (getter) that returns constant data. 这是一个古老的问题,答案很简单:只需创建一个返回常量数据的静态方法(getter)。
Instead of using: 而不是使用:
data(ls_sample) = lcl_myclass=>cs_data.
Use: 采用:
data(ls_sample) = lcl_myclass=>cs_data( ).
我将创建一个属性并将其标记为“只读”,您可以通过构造函数或使用Set-Method进行设置。
You cannot do like this using class constants in ABAP. 您不能在ABAP中使用类常量来这样做。 The documentation explicitly says that: 该文档明确指出:
You can specify a start value val for the ABAP types string and xstring only. 您只能为ABAP类型的string和xstring指定一个起始值val。
Constant internal tables, reference variables, and structures with not purely character-like flat components can be assigned their initial value by IS INITIAL only, and are therefore always initial. 常量内部表,参考变量和不具有纯字符型平面组件的结构只能通过IS INITIAL分配其初始值,因此始终为初始值。
As Tapio suggested, your only choice is read-only attributes, and I also suggest you to use static attributes, which can be initialized in constructor. 正如Tapio所建议的那样,您唯一的选择是只读属性,我还建议您使用静态属性,该属性可以在构造函数中初始化。
For example 例如
CLASS lcl_test DEFINITION.
PUBLIC SECTION.
CLASS-DATA: itab TYPE RANGE OF i READ-ONLY.
METHODS:
constructor.
ENDCLASS.
CLASS lcl_test IMPLEMENTATION.
METHOD constructor.
itab = VALUE #( sign = 'I' option = 'BT' ( low = 1 high = 10 )
( low = 21 high = 30 )
( low = 41 high = 50 )
option = 'GE' ( low = 61 ) ).
ENDMETHOD.
ENDCLASS.
One thing that after all that time would be a workaroud is the following: 在所有这些时间之后,将是一件令人费解的事情:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.