神一样的 Abap include Structure

刚看到abap的结构体时,感觉它和其它的语言没有什么分别。但是看到include的用法时,我服了,因为很多书都是一笔带过。

include 的主要作用是实现结构体的继承关系,详见如下例子:

"人员结构休类型
TYPES : BEGIN OF personinfo ,
  sid TYPE string,
  name TYPE string,
END OF personinfo.


"员工信息类型
TYPES : BEGIN OF staffinfo ,
  email TYPE string.
INCLUDE TYPE personinfo AS pinfo .
TYPES END OF staffinfo.


通过INCLUDE, staffinfo继承了personinfo的字段。

注意此时的BEGIN OF 和 END OF,都是以"."结束的,是一个完整的语句。中间包含的INCLUDE子句也是完整的语句。其自身的字段,则是BEGIN OF 或 TYPES END OF的组成部分。

因此如下形式也是正确的:

"员工信息类型
TYPES : BEGIN OF staffinfo . 
INCLUDE TYPE personinfo AS pinfo .
TYPES : email TYPE string,
END OF staffinfo.

"员工信息类型
TYPES : BEGIN OF staffinfo ,
  email TYPE string.
INCLUDE TYPE personinfo AS pinfo .
TYPES : age type i , END OF staffinfo .

DATA luke TYPE staffinfo .
luke- sid = '532525198504040913' .
luke- name = 'luke' .
luke- email = '[email protected]' .
WRITE : / , luke- sid ,luke - name, luke -email .
WRITE : / , luke- pinfo -sid , luke- pinfo -name , luke- email .  这咱语法也是正确的。

考虑一种场景,同时包括相同的类型多次:

"员工信息类型
TYPES : BEGIN OF staffinfo ,
  email TYPE string.
INCLUDE TYPE personinfo AS pinfo1 .

  INCLUDE TYPE personinfo AS pinfo2 .

TYPES : age type i , END OF staffinfo .

此时 luke- sid 将出现语法错误.解决方案:RENAMING WITH SUFFIX 选项,为其加上后缀。

"员工信息类型
TYPES : BEGIN OF staffinfo ,
  email TYPE string.
INCLUDE TYPE personinfo AS pinfo1 RENAMING WITH SUFFIX _pinfo1 .
INCLUDE TYPE personinfo AS pinfo2 RENAMING WITH SUFFIX _pinfo2 .
TYPES : age type i , END OF staffinfo .
DATA luke TYPE staffinfo .
luke- sid_pinfo1 = '532525198504040913' .
luke- name_pinfo1 = 'luke' .
luke- sid_pinfo2 = '532525198504040913_pinfo2' .
luke- name_pinfo2 = 'luke2' .
luke- email = '[email protected]' .
WRITE : / , luke- sid_pinfo1 ,luke - sid_pinfo2, luke -name_pinfo1 , luke- email .

以上部分是使用types声明的,也可以换成data关键字

"人员结构休类型
types : begin of PersonInfo ,
  SID type string,
  Name type string,
end of PersonInfo.
"员工信息类型
data : BEGIN OF staffinfo ,
  email TYPE string.
INCLUDE type PersonInfo as pInfo RENAMING WITH SUFFIX _pinfo .
data : age type i , END OF staffinfo .
DATA luke like staffinfo .
luke- sid_pinfo = '532525198504040913' .
luke- name_pinfo = 'luke' .
luke- pInfo- sid ='' .
luke- email = '[email protected]' .

"人员结构休类型
data : begin of PersonInfo ,
  SID type string,
  Name type string,
end of PersonInfo.
"员工信息类型
data : BEGIN OF staffinfo ,
  email TYPE string.
INCLUDE structure PersonInfo as pInfo RENAMING WITH SUFFIX _pinfo.
data : age type i , END OF staffinfo .
DATA luke like staffinfo .
luke- sid_pinfo = '532525198504040913' .
luke- name_pinfo = 'luke' .
luke- pInfo- sid ='' .
luke- email = '[email protected]' .

Structure还可以嵌套其它Structure,如下:

"员工信息类型
data : BEGIN OF staffinfo ,
BEGIN OF birthday ,
Date type d ,
time type t ,
END OF birthday ,   
  email TYPE string.
INCLUDE type PersonInfo as pInfo RENAMING WITH SUFFIX _pinfo .
data : age type i , END OF staffinfo .
DATA luke like staffinfo .
luke- sid_pinfo = '532525198504040913' .
luke- name_pinfo = 'luke' .
luke- pInfo- sid ='' .
luke- email = '[email protected]' .
luke- birthday- Date ='20120101' .

 

参考:

http://www.freesaptutorials.com/sap/abap-structures-include/

http://my.oschina.net/MikeLiuzheqiang/blog/84839

转载于:https://www.cnblogs.com/ITHelper/archive/2013/01/05/2846045.html

你可能感兴趣的:(神一样的 Abap include Structure)