S4HANA For ABAP(2):ABAP Expression改变

8 篇文章 5 订阅

1.8 ABAP Expression:Understanding The Move From Statements To Expressions

理解命令式语句和声明式语句差异;

示例代码:

"test2
"命令式与声明式语句
FORM f_test2.
  DATA:lv_val1 TYPE P LENGTH 10 DECIMALS 2.
  DATA:lv_val2 TYPE P LENGTH 10 DECIMALS 3.
  DATA:lv_cvar1 TYPE C LENGTH 20.
  lv_val1 = '10.2'.
  lv_val2 = '20.2'.
  lv_cvar1 = 'Test,String'.
  "命令式语句
  ADD 1 TO lv_val1.
  "声明式语句
  "lv_val1 = lv_val1 + 1.
  "运算比较表达式
  IF lv_val1 + 30 > lv_val2 * 2.
    WRITE:/ 'true'.
  ENDIF.
  "嵌套表达式
  lv_cvar1 = replace( val = lv_cvar1
                      sub = 'String'
                      with = to_upper( 'Replaced' ) ).
ENDFORM. 

1.9 ABAP Expression:Processing Strings Using Expressions and Functions

7.02版本开始,可以通过'&&'符号进行字符串连接操作;

CONCATENATE只支持字符类型C, N, D, T, STRING的连接;

'&&'支持任意类型;

 

String模板改变:

 

新增String Function:

 

1.10 ABAP Expression:Using Inline Declarations

Inline数据对象定义;

示例代码:

"test3
"Using Inline Declarations
FORM f_test3.
  DATA:lt_spfli TYPE TABLE OF spfli.
  DATA:ls_spfli LIKE LINE OF lt_spfli.
  SELECT * FROM spfli INTO TABLE lt_spfli.
  "使用前声明数据对象
  LOOP AT lt_spfli INTO ls_spfli.
    WRITE:/ ls_spfli-carrid,ls_spfli-connid.
  ENDLOOP.
  "在使用时声明数据对象
  LOOP AT lt_spfli INTO DATA(ls_spfli2).
    WRITE:/ ls_spfli-carrid,ls_spfli-connid.
  ENDLOOP.
  LOOP AT lt_spfli ASSIGNING FIELD-SYMBOL(<fs_spfli2>).
    WRITE:/ <fs_spfli2>-carrid,<fs_spfli2>-connid.
  ENDLOOP.
  "READ TABLE语句
  READ TABLE lt_spfli INTO DATA(ls_spfli3) INDEX 1.
  READ TABLE lt_spfli ASSIGNING FIELD-SYMBOL(<fs_sfpli3>) INDEX 1.
  "声明后,后续也能使用
  WRITE:/ ls_spfli2-carrid,ls_spfli2-connid.
ENDFORM.

1.11 ABAP Expression:Creating Objects and Casting Using Expressions

 

使用NEW创建Objects;

 

嵌套使用NEW;

 

7.40版本,CAST类型转换;

 

7.50版本,IS INSTANCE OF判断一个数据对象是否是另一个类对象类型实例;

 

7.50版本,CASE TYPE OF,WHEN TYPE,判读数据对象的类对象类型;

1.12 ABAP Expression:Assigning Values Using Expressions

 

使用VALUE()实现初始化赋值;

示例代码:

"test4
"VALUE,EXACT等额外操作符
FORM f_test4.
  DATA:lt_spfli TYPE TABLE OF spfli.
  DATA:ls_spfli LIKE LINE OF lt_spfli.
  DATA:lt_rcarrid TYPE RANGE OF spfli-carrid.
  DATA:ls_rcarrid LIKE LINE OF lt_rcarrid.
  TYPES:BEGIN OF s_ty1,
        col1 TYPE C LENGTH 10,
        col2 TYPE I,
        col3 TYPE I,
        END OF s_ty1.
  DATA:lt_ty1 TYPE TABLE OF s_ty1.
  DATA:ls_ty1 LIKE LINE OF lt_ty1.

  SELECT * FROM spfli INTO TABLE lt_spfli.
  TRY.
  "inline declaration:go_alv
  cl_salv_table=>factory( IMPORTING r_salv_table = DATA(go_alv)
                          CHANGING t_table = lt_spfli ).
  "使用VALUE()
  lt_rcarrid = VALUE #( ( sign = 'I' option = 'EQ' low = 'AA' )
                            ( sign = 'I' option = 'EQ' low = 'AB' ) ).
  lt_ty1 = VALUE #( col1 = 'TEST' ( col2 = 10 col3 = 20 ) ( col2 = 22 col3 = 33 ) ).
  cl_salv_table=>factory( IMPORTING r_salv_table = DATA(go_alv1)
                          CHANGING t_table = lt_rcarrid ).
  "这种方式不支持CHANGING参数,只支持IMPORTING
*  cl_salv_table=>factory( IMPORTING r_salv_table = DATA(go_alv2)
*                          CHANGING t_table = VALUE #(
*                            ( sign = 'I' option = 'EQ' low = 'AA' )
*                            ( sign = 'I' option = 'EQ' low = 'AB' ) ) ).

  "使用EXACT()
  DATA:lv_num TYPE N LENGTH 4.
  DATA:lv_char TYPE C LENGTH 4.
  lv_char = 'AB12'.
  lv_num = lv_char.
  "lv_num = 0012,AB loss
  WRITE:/ lv_num.
  TRY.
  lv_num = EXACT #( lv_char ).
  CATCH cx_sy_conversion_no_number INTO DATA(lo_conversion_exception).
    "模板输出
    WRITE:/ |{ lo_conversion_exception->get_text( ) }|.
  ENDTRY.
  "使用CORRESPONDING()
  TYPES:BEGIN OF s_spfli,
        carrid TYPE spfli-carrid,
        connid TYPE spfli-connid,
        test TYPE C LENGTH 10,
        END OF s_spfli.
  DATA:lt_spfli2 TYPE TABLE OF s_spfli.
  DATA:ls_spfli2 LIKE LINE OF lt_spfli2.
  lt_spfli2 = CORRESPONDING #( lt_spfli ).
  LOOP AT lt_spfli2 INTO ls_spfli2.
    WRITE:/ ls_spfli2-carrid,ls_spfli2-connid.
  ENDLOOP.

  DATA:lv_val1 TYPE string.
  DATA:lv_val2 TYPE string.
  lv_val1 = 'TEST'.
  "使用COND(),if else
  lv_val2 = COND #(
              WHEN lv_val1 = 'TEST' THEN 'test value'
              WHEN lv_val1 = 'TEXT' THEN 'text value'
              ELSE 'other value'
              ).
  "使用SWITCH(),case when
  lv_val2 = SWITCH #( lv_val1
              WHEN 'TEST' THEN 'test value'
              WHEN 'TEXT' THEN 'text value'
              ELSE 'other value'
            ).
  CATCH cx_salv_msg INTO DATA(lo_exception).
    "模板输出
    WRITE:/ |{ lo_exception->get_text( ) }|.
  ENDTRY.
ENDFORM. 

使用REF(),获取数据对象指针;

 

使用CONV(),进行数据类型转换;

 

使用EXACT,进行没有精度损失的赋值;

 

使用CORRESPONDING(),实现MOVE-CORRESPONDING赋值;

 

使用COND(),实现条件表达式,类似IF ELSE效果;

 

使用SWITCH(),实现条件表达式,CASE WHEN效果;

1.13 ABAP Expression:Using Enumerations

定义枚举类型;

 

当赋值不在枚举类型可用值时会报错;

示例代码:

"test5
"枚举类型
FORM f_test5.
  TYPES:BEGIN OF ENUM enum1,
        apple,
        orange,
        banana,
        END OF ENUM enum1.
  DATA:lv_fruit TYPE enum1.
  lv_fruit = apple.
  "编译报错
  "lv_fruit = 'test'.
  IF lv_fruit = apple.
    WRITE:/ 'true'.
  ENDIF.
ENDFORM.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

偶是不器

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值