ABAP结构体学习

2021-11-09 18:27:58 浏览数 (1)

一、定义结构体类型

利用下面语法可以定义由多个字段组成的结构体类型

代码语言:javascript复制
TYPES BEGIN OF struc_type
		...
		{TYPES dtype...} | {INCLUDE {TYPE|STRUCTURE}...},
		...
TYPES END OF struc_type.		

二、声明结构体类型

声明复合型结构体类型,并利用此类型定义结构体变量的方法

代码语言:javascript复制
*声明结构体类型
TYPES:BEGIN OF T_PEOPLE,
        NAME    TYPE C LENGTH 20,
        COUNTRY TYPE C LENGTH 15,
        CITY    TYPE C LENGTH 10,
      END OF T_PEOPLE.

*定义结构体变量
DATA GS_PEOPLE   TYPE T_PEOPLE.
GS_PEOPLE-NAME = 'NAME'.
GS_PEOPLE-COUNTRY = 'COUNTRY'.
GS_PEOPLE-CITY = 'Beijing'.

三、嵌套结构体类型

利用INCLUDE STRUCTRER语句,可以把DATA语句定义的结构体或者ABAP数据字典存在的结构体包含到嵌套结构体中。

代码语言:javascript复制
*声明结构体类型
TYPES:BEGIN OF T_PEOPLE,
        NAME    TYPE C LENGTH 20,
        COUNTRY TYPE C LENGTH 15,
        CITY    TYPE C LENGTH 10,
      END OF T_PEOPLE.

*定义结构体变量
DATA GS_PEOPLE   TYPE T_PEOPLE.
GS_PEOPLE-NAME = 'NAME'.
GS_PEOPLE-COUNTRY = 'COUNTRY'.
GS_PEOPLE-CITY = 'Beijing'.

*定义嵌套结构体变量
DATA:BEGIN OF GS_PEOPLE1.
    INCLUDE STRUCTURE GS_PEOPLE AS PEO.
DATA:PHONE TYPE C LENGTH 10,
     END OF GS_PEOPLE1.

GS_PEOPLE1-PEO-NAME = '123'.
WRITE   GS_PEOPLE1-PEO-NAME.

四、包含结构体类型

如果在INCLUDE STRUCTURE中不使用 as 语句,那么则是直接包含结构体,包含后使用各个项目时,要直接用第一级别的结构体名定义字段名。

代码语言:javascript复制
*定义包含结构体变量
DATA:BEGIN OF GS_PEOPLE2.
    INCLUDE STRUCTURE GS_PEOPLE .
DATA:PHONE TYPE C LENGTH 10,
     END OF GS_PEOPLE2.
GS_PEOPLE2-NAME = '456'.
WRITE   GS_PEOPLE2-NAME.

0 人点赞