1. 程式人生 > >ABAP-資料引用

ABAP-資料引用

*&---------------------------------------------------------------------*
*& Report  ZRICO_TEST3
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
report zrico_test3.

data:ztb type 
string, zfd type string. data:gt_tab like table of mara with header line. start-of-selection. ztb = 'MARA'. zfd = 'MATNR'. field-symbols:<zs> type mara, <zl> type any, <z1> type any. data:dref type ref to data. create data dref type (ztb). assign dref
->* to <zs>. assign component zfd of structure <zs> to <zl>. types:begin of t_struct, "*定義一個結構* col1 type i, col2 type i, end of t_struct. data: dref1 type ref to data, "*定義兩個資料引用* dref2 type ref to data. field
-symbols: <fs1> type t_struct, "*定義兩個欄位符號* <fs2> type i. create data dref1 type t_struct. "*利用資料引用,動態建立物件* assign dref1->* to <fs1>. "*因為資料引用只是地址,不能直接操作使用,需要把它分配給欄位符號* <fs1>-col1 = 1. "*賦值給元件欄位* <fs1>-col2 = 2. dref2 = dref1. "*把一個資料引用賦值給另一個資料引用* assign dref2->* to <fs1>. "*為了要輸出,需要把資料應用分配給欄位符號* write: / 'DREF2',<fs1>-col1, <fs1>-col2. "*可以直接輸出了* assign dref2->* to <fs2> casting. "*把結構變數分配給整形欄位符號,因此要進行隱式資料型別轉換* write / <fs2>. "*輸出<FS2>欄位符號變數內容* get reference of <fs1>-col2 into dref2. "*取得變數的資料應用,給DREF2* assign dref2->* to <fs2>. "*分配變數給欄位符號<FS2>* write / <fs2>.