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

时间:2013-12-17 15:21:11

标签: enums sap abap

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

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

4 个答案:

答案 0 :(得分:7)

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

  1. 创建“enum”类;
  2. 将实例构造函数设置为private;
  3. 为每个“枚举对象”添加TYPE REF TO <your_class>的静态属性;
  4. 创建CLASS_CONSTRUCTOR并使用所需属性实例化每个“枚举对象”。
  5. 例如,如果您有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.
    

答案 1 :(得分: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'.

答案 2 :(得分: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.

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

答案 3 :(得分:0)

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

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