[SAP ABAP开发技术总结]结构复用(INCLUDE)

 

TYPES BEGIN OF struc_type.
  
TYPES comp ...
   TYPES
comp TYPE struc_type BOXED. "参照另一结构类型
  
INCLUDE { {TYPE struc_type} | {STRUCTURE struc} } "将另一结构包括进来
        [
AS name [RENAMING WITH SUFFIX suffix]].
TYPES END OF struc_type.

 

INCLUDE { {TYPE struc_type} | {STRUCTURE struc} }
        [
AS name [RENAMING WITH SUFFIX suffix]]
.

该语句只能用在定义结构的BEGIN OF END OF之间。作用是将结构类型struc_type结构变量struc的所有组件字段拷贝到当前结构定义的指定位置,INCLUDE就是将可以重复使用的东西先做好,再包含进来。

AS name:给包含进来的结构类型(或结构变量)取一个别名,这样就可以通过结构组件符(-)来选取这个结构类型(或结构变量)

RENAMING WITH SUFFIX suffix:如果include进来的结构类型(或结构变量)的组件字段与现有的重复,则可以使用此选项重命名include进来的结构类型(或结构变量)的各组件字段名,具体做法只是在原来组件名后加上了指定的后缀suffix

TYPES: BEGIN OF t_day,
        
work TYPE c LENGTH 8,
        
free TYPE c LENGTH 16,
      
END OF t_day.
DATA BEGIN OF week.
       
INCLUDE TYPE t_day AS monday    RENAMING WITH SUFFIX _mon.
       
INCLUDE TYPE t_day AS tuesday   RENAMING WITH SUFFIX _tue.
       
INCLUDE TYPE t_day AS wednesday RENAMING WITH SUFFIX _wed.
...
DATA END OF week.

可以通下面的方式来访问week结构变量:

直接看作是week结构变量组件:week-work_mon, week-free_mon, week-work_tue

由于使用as别名,所以还可以这样访问:week-monday-work, week-monday-free, week-tuesday-work

 

当程序中多个结构使用共同的字段时,将公用的部分提取出来,使用INCLUDE将它们组装起来,编程结构更清晰。下面是结构对象的复用:

DATA: BEGIN OF comm1 OCCURS 0,
      bukrs
TYPE bseg-bukrs,
END OF comm1.
TYPES:BEGIN OF comm2,
      blart
TYPE bkpf-blart,
END OF comm2.
DATA: BEGIN OF gt_result OCCURS 0,
        c1
TYPE c."直接定义组件字段,但前面语句后面使用逗号

       
INCLUDE STRUCTURE comm1."直接将结构对象包括进来
       
INCLUDE TYPE comm2."直接将结构类型包括进来
       
DATA:comm LIKE comm1,"直接参照
        c2
TYPE c. "直接定义组件字段,但前面语句后面使用逗号
DATA: END OF gt_result.
gt_result
-bukrs = '111'.
gt_result
-blart = '222'.
gt_result
-comm-bukrs = '333'.

下面是类型的复用:

TYPES: BEGIN OF street_type,
         name
TYPE c LENGTH 40,
        
no   TYPE c LENGTH 4,
      
END OF street_type.
DATA: BEGIN OF comm1 OCCURS 0,
      bukrs
TYPE bseg-bukrs,
END OF comm1.
TYPES: BEGIN OF address_type,
        name1
TYPE c LENGTH 30."直接定义类型,但前面语句需使用逗号

       
TYPES:street TYPE street_type,"参照另一结构类型
       
c TYPE c."直接定义类型,但前面语句需使用逗号

       
INCLUDE STRUCTURE comm1.
       
INCLUDE TYPE  street_type.
TYPES: END OF address_type.

*或者是这样
*TYPES: BEGIN OF address_type,
*        name1 TYPE c LENGTH 30,
*        street TYPE street_type,
*        c TYPE c.
*        INCLUDE STRUCTURE comm1.
*        INCLUDE TYPE  street_type.
*TYPES: END OF address_type.
DATA: name TYPE address_type-street-name.
DATA: name2 TYPE address_type-name.
DATA: bukrs TYPE address_type-bukrs.

posted @ 2015-02-01 16:38  江正军  阅读(19402)  评论(1编辑  收藏  举报