ABAP 7.4新特性(三): VALUE 操作符

关于VALUE前面也提到过了,其主要是用来给一个结构、内表等对象初始化值。主要有下面三个方式:

...value dtype\#()  构造一个任意类型的初始值
...value dtype\#( comp1 = a1 comp2 = a2 ....) 构造一个任意类型的结构体的初始值
...value dtype\#( ( ...) (...) ... )...  构造一个任意类型的内表的初始值

上面dtype\# 意思是可以指定一个dtype的类型或者直接用#,用#时,其类型和被赋值的对象的数据类型一样。

如果value后的dtype是个表,则必须指定key值,或者声明为empty key。http://scn.sap.com/community/abap/blog/2013/06/27/abap-news-for-release-740--internal-tables-with-empty-key。

When you declare a standard table data object without specifiying the primary key, the default key is taken. The default key consists of all character and byte like fields of the table structrure. If the structure contains only numeric fields, duh! The same would have happened if you declared the DEFAULT KEY explicitly. But note that an empty key is not possible for sorted and hashed tables.

Without explicit key declaration the type would not be usable for the inline data declaration shown here. Since I don't care about the key, I use the empty key. A SORT itab without specifying a sort key will do nothing and produce a warning from the syntax check.

 

Starting with release 7.40 you declare your standard tables either with a good key or an empty key but never with the chancy default key!

例1

CLASS c1 DEFINITION.
  PUBLIC SECTION.
   TYPES: BEGIN OF t_struct,
              col1 TYPE i,
              col2 TYPE i,
          END OF t_struct.
   CLASS-METHODS m1 IMPORTING p TYPE t_struct.
   ENDCLASS.

CLASS c1 IMPLEMENTATION.
  METHOD m1.
    WRITE:'col1:' , p-col1,'col2:',p-col2.
  ENDMETHOD.
  ENDCLASS.

START-OF-SELECTION.
  c1=>m1( VALUE #( col1 = 1 col2 = 2 ) ).
输出就是 col:1  col:2.

例2

DATA itab TYPE RANGE OF i.
itab = VALUE #( sign = 'I' option = 'BT' ( low = 1 high = 10 )
                                         ( low = 21 high = 30 )
                                         ( low = 41 high = 50 )
                           option = 'GE' ( low = 61 ) ).
cl_demo_output=>DISPLAY( itab ).


例3

TYPES t_date_tab TYPE table of string with empty key.
DATA(date_tab) = VALUE t_date_tab(
  ( | { conv d( sy-datlo - 1 ) DATE = environment } | ) 
  ( | {         sy-datlo       DATE = environment } | )
  ( | { conv d( sy-datlo + 1 ) DATE = environment } | ) ).

cl_demo_output=>DISPLAY( date_tab ).


这里DATE environment是用来格式化日期的,表达式里必须有日期类型d才能使用。environment对应的值是CL_ABAP_FORMAT=>D_ENVIRONMENT

另外对日期进行加减时还需conv。如果第二行不用DATE environment,那么输出就是20160606这样的格式。关于嵌入表达式可以F1看看

例4

为每一行赋值指定列名的好处:可以指定某一行的某一列都是同一个值而不用每一行都要输入该列值。




补充:


  • 8
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值