TYPES关键字用于创建自定义数据类型,就像JAVA里面创建类一样,用TYPES创建的数据类型可以被其它变量引用(类似于实例化对象),而本身不能直接引用或者赋值。
DATA关键字用于定义结构体,可以自定义结构体或者参考某一个自定义类型创建对象,我把它称为实例化对象。
[For Example]
TYPES:BEGIN OF ILINE, "創建一個類型,包含三個字段 TYPES:定義的是類型 DATA:則為對象
FIRST_NAME(30) TYPE C,
LAST_NAME(30) TYPE C,
AGE TYPE I,
END OF ILINE.
DATA WA TYPE ILINE. "定义一个结构
DATA ITAB TYPE ILINE OCCURS 0 WITH HEADER LINE. "參數某個結構創建的包含HEADER LINE的內表
DATA JTAB TYPE ILINE OCCURS 0. "參照某個結構創建的無表HEADER LINE的內表,如果單獨創建(不參照某個結構)則包含表頭,如下创建自定义结构.
DATA: BEGIN OF OTAB OCCURS 0, "以此种方式自定义的结构默认带HEADER LINE
FIRST_NAME(30) TYPE C,
LAST_NAME(30) TYPE C,
AGE TYPE I,
END OF OTAB.
ITAB-FIRST_NAME = 'VANCY'.
ITAB-LAST_NAME = 'WEI'.
ITAB-AGE = 10.
APPEND ITAB.
ITAB-FIRST_NAME = 'AOBAMA'.
ITAB-LAST_NAME = 'WEI'.
ITAB-AGE = 10.
APPEND ITAB.
ITAB-FIRST_NAME = 'LIKEM'.
ITAB-LAST_NAME = 'WEI'.
ITAB-AGE = 10.
APPEND ITAB.
LOOP AT ITAB.
WRITE:/ 'SY-TABIX=',SY-TABIX,
'FIRST_NAME=',ITAB-FIRST_NAME,
'LAST_NAME=',ITAB-LAST_NAME,
'AGE=',ITAB-AGE LEFT-JUSTIFIED.
ENDLOOP.
*無表頭的只能通過WORK AREA來添加數據進入內表,而有表頭的兩種方式都可以用
WA-FIRST_NAME = 'SUNDAY'.
WA-LAST_NAME = 'WEI'.
WA-AGE = 10.
APPEND WA TO JTAB.
WA-FIRST_NAME = 'MONDAY'.
WA-LAST_NAME = 'WEI'.
WA-AGE = 10.
APPEND WA TO JTAB.
WA-FIRST_NAME = 'TUESDAY'.
WA-LAST_NAME = 'WEI'.
WA-AGE = 10.
APPEND WA TO JTAB.
LOOP AT JTAB INTO WA.
WRITE:/ 'SY-TABIX=',SY-TABIX,
'FIRST_NAME=',WA-FIRST_NAME,
'LAST_NAME=',WA-LAST_NAME,
'AGE=',WA-AGE LEFT-JUSTIFIED.
ENDLOOP.
OTAB-FIRST_NAME = 'JANUARY'.
OTAB-LAST_NAME = 'WEI'.
OTAB-AGE = 1.
APPEND OTAB.
OTAB-FIRST_NAME = 'FEBRUARY'.
OTAB-LAST_NAME = 'WEI'.
OTAB-AGE = 10.
APPEND OTAB.
OTAB-FIRST_NAME = 'MARCH'.
OTAB-LAST_NAME = 'WEI'.
OTAB-AGE = 10.
APPEND OTAB.
LOOP AT OTAB.
WRITE:/ 'SY-TABIX=',SY-TABIX,
'FIRST_NAME=',OTAB-FIRST_NAME,
'LAST_NAME=',OTAB-LAST_NAME,
'AGE=',OTAB-AGE LEFT-JUSTIFIED.
ENDLOOP.
程序运行界面如下: