是否可以在ABAP中创建枚举(枚举)?

问题描述 投票:5回答:4

是否可以在ABAP中创建Enumeration(枚举),例如在Java中?

据我所知,我们可以定义一个domain with fixed values,但它们仅用于屏幕。

enums sap abap
4个回答
7
投票

在7.51版之前,ABAP中没有对枚举的本机支持,但您可以通过几个简单的步骤来模拟相同的行为:

  1. 创建你的“enum”课程;
  2. 将实例构造函数设置为private;
  3. 为每个“枚举对象”添加TYPE REF TO <your_class>的静态属性;
  4. 创建一个CLASS_CONSTRUCTOR并使用所需的属性实例化每个“枚举对象”。

例如,如果你有一个status枚举,你可能有一个CL_STATUS类与CL_STATUS=>APPROVEDCL_STATUS=>REJECTED枚举对象。

例:

REPORT z_test.

CLASS cl_status DEFINITION
      CREATE PRIVATE.
  PUBLIC SECTION.
    CLASS-DATA: approved TYPE REF TO cl_status,
                rejected TYPE REF TO cl_status.
    CLASS-METHODS class_constructor.
ENDCLASS.
CLASS cl_status IMPLEMENTATION.
  METHOD class_constructor.
    approved = NEW cl_status( ).
    rejected = NEW cl_status( ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  DATA status TYPE REF TO cl_status.
  status = cl_status=>approved.
  CASE status.
    WHEN cl_status=>approved.
      MESSAGE 'approved' TYPE 'I'.
    WHEN cl_status=>rejected.
      MESSAGE 'rejected' TYPE 'I'.
  ENDCASE.

3
投票

从ABAP版本7.51开始,也可以在ABAP中使用enumerated objects

有关更多信息,请参阅此blog

简单的例子:

TYPES:
  BEGIN OF ENUM ty_enum_status STRUCTURE status,
    approved,
    rejected,
  END OF ENUM ty_enum_status STRUCTURE status.

DATA my_status TYPE ty_enum_status.
my_status = status-approved.

表格示例:

TYPES:
  BEGIN OF ENUM ty_enum_mealtype STRUCTURE mealtype BASE TYPE smeal-mealtype,
    unknown    VALUE IS INITIAL,
    vegetarian VALUE 'VE',
    fish       VALUE 'FI',
    flesh      VALUE 'FL',
  END OF ENUM ty_enum_mealtype STRUCTURE mealtype.

SELECT SINGLE * FROM smeal
  WHERE carrid     = 'LH'
    AND mealnumber = 1
  INTO @DATA(smeal).

" For comparing an enumerated component with a data object of the base type,
" it requires a conversion to the enumerated type
DATA(enum_mealtype) = SWITCH #( CONV ty_enum_mealtype( smeal-mealtype )
  WHEN mealtype-vegetarian THEN mealtype-fish
  WHEN mealtype-fish       THEN mealtype-flesh
  WHEN mealtype-flesh      THEN mealtype-vegetarian ).

" Conversion between ENUM type and string type - This takes the first characters 
" of the name of the enumerated component
ASSERT CONV string( enum_mealtype ) = SWITCH #( enum_mealtype
  WHEN mealtype-unknown    THEN `UNKNOWN`
  WHEN mealtype-vegetarian THEN `VEGETARIAN`
  WHEN mealtype-fish       THEN `FISH`
  WHEN mealtype-flesh      THEN `FLESH` ).

" Assigning an enumerated variable to a data object of the base type
smeal-mealtype = CONV smeal-mealtype( enum_mealtype ).

" Conversion between ENUM type and base type
ASSERT CONV smeal-mealtype( enum_mealtype ) = SWITCH #( enum_mealtype
  WHEN mealtype-unknown    THEN '  '
  WHEN mealtype-vegetarian THEN 'VE'
  WHEN mealtype-fish       THEN 'FI'
  WHEN mealtype-flesh      THEN 'FL' ).

" Use CONV for assigning an enumerated component to a data object of the base type
smeal-mealtype = CONV #( mealtype-vegetarian ).
ASSERT smeal-mealtype = 'VE'.

" If you don't use CONV, you'll get the first characters of the name of the 
" enumerated component
smeal-mealtype = mealtype-unknown.
ASSERT smeal-mealtype = 'UN'.

1
投票

稍微接近一点的方法是使用以下方法;

constants: begin of status,
             approved TYPE i value 1,
             rejected TYPE i value 2,
           end of status.

现在,您在整个程序中使用此常量:

DATA my_status TYPE i.
my_status = status-approved.
CASE my_status.
  WHEN status-approved.
    MESSAGE 'approved' TYPE 'I'.
  WHEN status-rejected.
    MESSAGE 'rejected' TYPE 'I'.
ENDCASE.

如果您希望它们是全局的,请在全局类中将它们创建为类属性。


0
投票

不,没有办法在ABAP中创建枚举。虽然在编译时检查枚举,但域显然不能 - 域和使用它的程序可以完全单独修改和传输。

您将获得的最接近的是使用域对照域值验证任何数据,在很多情况下手动验证(正如您所指出的,经典dynpro屏幕将自动检测并自动检查固定值 - 对于CHAR和NUMC好歹)。

© www.soinside.com 2019 - 2024. All rights reserved.