简体   繁体   English

如何在ABAP对象中将填充的结构/表定义为类常量

[英]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: 在所有这些时间之后,将是一件令人费解的事情:

  • Create your list 建立清单
  • serialize it and save it as a read-only string 序列化并将其另存为只读字符串
  • create a getter that deserializes it 创建一个反序列化的吸气剂

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 python 中使用 for 循环定义多个 class 对象? - How can I define multiple class objects with a for loop in python? 如何连接常量和变量并使用PHP将其存储在类常量中? - How can I concatenate a constant and a variable and store it in a class constant with PHP? 如何在 OO ABAP 中使用具有 3 个不同单选按钮的动态 SALV - How can I use dynamic SALV with 3 different raditobutton in OO ABAP 如何将派生类的对象存储在可以访问基本 class 没有的属性的数据结构中? - How can I store objects of derived classes within a data structure from which can be accessed attributes which the base class does not have? 如何创建具有恒定属性的类,该类属性可用于包文件夹结构中的其他类? - How do I create a class with constant properties accessible to other classes within a package folder structure? 如何在Symfony 4中定义一个名为class的未定义方法“ handleRequest”? - How can I define an undefined method named “handleRequest” of class in Symfony 4? 如何在R中定义一个用于存储用户定义函数的类? - How can I define a class in R for storing user defined functions? 可以通过用户定义构造器联合包含类的对象吗? - Can union contain objects of a class with user define constructor? 在 OOP 中,我应该为表的写入和读取定义不同的对象吗? - In OOP, should I define different objects for writing to and reading from a table? 强制实现接口的类定义常量 - Force a class implementing the interface to define a constant
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM