壹佰网|ERP100 - 企业信息化知识门户

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1114|回复: 2

[ABAP] 【ABAP动态编程】ABAP中动态编程 (第一部分)

[复制链接]
发表于 2012/8/14 23:14:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。如果您注册时有任何问题请联系客服QQ: 83569622  。

您需要 登录 才可以下载或查看,没有帐号?注册

x

在我们实际的ABAP开发中,我们经常会使用动态编程,如动态地获取运行参数,动态创建类型、内表等等。这样做给我们带来的好处之一,就是“扩充”了我们代码的重用性。
一.动态编程(Field Symbol以及casting)以field symbol为例:

*&———————————————————————*
*& Report ZTEST_FIELD_SYMBOL
*& Tang Can
*&———————————————————————*
*& Test Field symbo
*&———————————————————————*
REPORT ZTEST_FIELD_SYMBOL.
* define my teype
TYPES: typ_mine(10) type c.
* define Data type
DATA:
smallfield(5) type c,
largefield(15) type c VALUE ‘123456789012345′,
typename(8) type c value ‘TYP_MINE’,
sometype type REF TO CL_ABAP_TYPEDESCR,
rrt_type TYPE REF TO CL_ABAP_DATADESCR.
FIELD-SYMBOLS: TYPE any, type typ_mine.
* type==>type
ASSIGN largefield to CASTING.
WRITE:/ ‘Casting from 15==>10 to :’, .
ASSIGN largefield to CASTING type typ_mine.
WRITE:/ ‘Casting from 15==>10 to ,”Casting type to typ_mine” in Field-symbol:’, .
ASSIGN largefield to CASTING type n.
WRITE:/ ‘Casting from 15==>n to ,”Casting type to N” in Field-symbol:’, .
ASSIGN largefield to CASTING type (typename).
WRITE:/ ‘Casting from 15==>10 to ,”Dynamically Casting type to typ_mine” in Field-symbol:’, .
sometype = cl_abap_typedescr=>DESCRIBE_BY_NAME( ‘TYP_MINE’ ).
“ASSIGN largefield to CASTING TYPE HANDLE sometype.
rrt_type ?= sometype.
ASSIGN largefield to CASTING TYPE HANDLE rrt_type.
WRITE:/ ‘Casting from 15==>10 to ,”using CL_ABAP_TYPEDESCR” in Field-symbol:’, .
ASSIGN largefield to CASTING LIKE smallfield.
WRITE:/ ‘Casting from 15==>5 to ,”Like Smallfield” in Field-symbol:’, .
ASSIGN largefield to CASTING LIKE .
WRITE:/ ‘Casting from 15==>5 to ,”Like ” in Field-symbol:’, .



测试程序:

我们的运行结果:
17144169_201010171647001.jpg
二.动态编程(五类Token分类)1.Dynamic Field Specification
原理:
17144169_201010171648101.jpg

测试程序

*&———————————————————————*
*& Report ZTEST_DYN_FIELD
*&
*&———————————————————————*
*& test Dynamically field-specification
*&
*&———————————————————————*
REPORT ZTEST_DYN_FIELD.
* constance
CONSTANTS: a type i value 1,
b type i value 2.
* data
DATA:
name(5) TYPE c.
FIELD-SYMBOLS: type i.
*——————————–
* test
name = ‘A’.
ASSIGN (name) to .
write:/10 .
* test
name = ‘B’.
ASSIGN (name) to .
write:/10 .

结果:
17144169_201010171649421.jpg
2.Dynamic Type Specification
原理:
17144169_201010171650281.jpg

实现:
上面第一点已经总结。
17144169_201010171651211.jpg

3.Dynamic Component Specification
原理:
17144169_201010171652161.jpg
4.Dynamic Clause Specification
原理:
17144169_201010171653041.jpg


5.Dynamic Subroutine Specification
原理:
17144169_201010171653431.jpg


6.总结
测试dynamic field specification,dynamic Type Specification, dynamic Component Specification, Dynamic Clause specification:
测试程序:

*&———————————————————————*
*& Report ZTEST_DYN
*&—————————————————————–*
REPORT ZTEST_DYN.
DATA:
ref_data type REF TO data, “for creating internal table
itab_cond type TABLE OF string, “for condition
lw_name type string. “for write
FIELD-SYMBOLS: type ANY,
type any.
PARAMETERS:
p_tab_nm type string,
p_cond type string.
* create the internal-table of Parameter
CREATE DATA ref_data type (p_tab_nm).
ASSIGN ref_data->* to .
append p_cond to itab_cond.
* get the data of dynamic table
SELECT * from (p_tab_nm) into UP TO 1 ROWS
where (itab_cond).
ENDSELECT.
* write
write:/10 ‘Using ”Component Name” to output’.
lw_name = ‘CARRID’.
ASSIGN COMPONENT lw_name OF STRUCTURE to .
write:/10 lw_name,
30 .
lw_name = ‘CONNID’.
ASSIGN COMPONENT lw_name OF STRUCTURE to .
write:/10 lw_name,
30 .
lw_name = ‘TEXT’.
ASSIGN COMPONENT lw_name OF STRUCTURE to .
write:/10 lw_name,
30 .
write:/10 ‘———————————————’.
write:/10 ‘Using ”Indext” to output’.
ASSIGN COMPONENT 1 OF STRUCTURE to .
write:/10 ‘Index 1′,
30 .
ASSIGN COMPONENT 2 OF STRUCTURE to .
write:/10 ‘Index 2′,
30 .
ASSIGN COMPONENT 3 OF STRUCTURE to .
write:/10 ‘Index 3′,
30 .
我们使用数据:



17144169_201010171655061.jpg
结果:
画面输出
17144169_201010171655461.jpg
结果
719.jpg

官方PPT 文档资料附件中



ABAP351(高级编程).pdf

1.36 MB, 下载次数: 37, 下载积分: 努力值 -5 点

动态编程

点评

转自:http://scnblogs.techweb.com.cn/tcSAPbw/archives/363.html  发表于 2012/8/15 06:52

本帖被以下淘专辑推荐:

发表于 2012/9/25 14:15:47 | 显示全部楼层
学习第一篇
很好  
发表于 2012/12/8 23:40:53 | 显示全部楼层
这个一定要掌握
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|手机版|壹佰网 ERP100 ( 京ICP备19053597号-2 )

Copyright © 2005-2012 北京海之大网络技术有限责任公司 服务器托管由互联互通
手机:13911575376
网站技术点击发送消息给对方83569622   广告&合作 点击发送消息给对方27675401   点击发送消息给对方634043306   咨询及人才点击发送消息给对方138011526

GMT+8, 2025/11/29 12:05 , Processed in 0.041415 second(s), 20 queries , File On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表