Abap/4语法(5)

*&---------------------------------------------------------------------*
*& Report  Z_CPT3_6                                                    *
*&                                                                     *
*&---------------------------------------------------------------------*
*&             动态数据对象

*&                                                                     *
*&---------------------------------------------------------------------*

REPORT  Z_CPT3_6.

*文本字段
DATA:curDate TYPE D.
FIELD-SYMBOLS <fs> TYPE D.

curDate = '20120405'.
ASSIGN curDate TO <fs>.

WRITE: / curDate.

*指向结构体的文本字段
SKIP 1.
WRITE: / 'FIELD SYMBOL指向内表'.
TYPES: begin of t_address,
           no(5) TYPE n,
           street(30) TYPE c,
       end of t_address.
DATA itab_add TYPE TABLE OF t_address.
DATA address TYPE t_address.

FIELD-SYMBOLS <fs2> TYPE INDEX TABLE.
ASSIGN itab_add TO <fs2>.

address-no = '00001'.
address-street = '学府路'.
APPEND address TO <fs2>.

LOOP AT <fs2> INTO address.
    WRITE:/ address-no,address-street.
ENDLOOP.

*通过偏移量来赋值
SKIP 1.
WRITE: / '通过偏移量来赋值'.
DATA: begin of  address2,
          street(20) TYPE c VALUE 'Nanyang Street',
          no(4) TYPE c VALUE '1234',
          zip(5) TYPE c VALUE '56789',
          city(20) TYPE c Value 'Singaporc',
      end of address2.

FIELD-SYMBOLS:<street-no> TYPE c,
              <zip-city> TYPE c,
              <tmp> TYPE c.

ASSIGN: address2(24) TO <street-no>,
        address2+24(25) TO <zip-city>.

WRITE: / <street-no>,
         <zip-city>.


*测试文本字段是否只读字段,   结果:文本字段确实是个只读的指针。
SKIP 1.
WRITE / '测试文本字段是否只读字段'.
ASSIGN <street-no> TO <tmp>.
ASSIGN address2+10(10) TO <street-no>.
WRITE: / 'street-no:',<street-no>,
       / 'zip-city:',<zip-city>,
       / 'tmp:',<tmp>.
SKIP 1.
WRITE: / 'address2:',
       / address2-street,
       / address2-no,
       / address2-zip,
       / address2-city.

*动态分配文本符号
SKIP 1.
WRITE: / '动态分配文本符号'.
FIELD-SYMBOLS:<fs_3>.
DATA:str(20) TYPE c VALUE 'Output String',
     name(20) TYPE c VALUE 'str'.

ASSIGN name to <fs_3>.
WRITE / <fs_3>.

*加括号就是指:变量名
ASSIGN (name) TO <fs_3>.
WRITE / <fs_3>.







*数据引用
SKIP 1.
WRITE: / '数据引用'.
TYPES:begin of t_struct,
         col1 TYPE i,
         col2 TYPE i,
      end of t_struct.

*data 是什么类型?
DATA: dref1 TYPE REF TO data,
      dref2 TYPE REF TO data.

FIELD-SYMBOLS:<fs_1> TYPE t_struct,
              <fs_2> TYPE i.

*根据指针来创建类型
CREATE DATA dref1 TYPE t_struct.

ASSIGN dref1->* TO <fs_1>.
<fs_1>-col1 = 1.
<fs_1>-col2 = 2.
dref2 = dref1.
*赋值的时候用CASTING表示强制转换,允许截断操作
ASSIGN dref2->* TO <fs_2> CASTING.
WRITE / <fs_2>.
*将对象的地址赋值给指针
GET REFERENCE OF <fs_1>-col2 INTO dref2.
*将指针对象所指向的内容赋值给字段符合
ASSIGN dref2->* TO <fs_2>.
WRITE / <fs_2>.

你可能感兴趣的:(Abap/4语法(5))