1.【ABAP动态编程】ABAP中动态编程 (第一部分)范文

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
ASSIGNlargefieldto<any> CASTINGtypetyp_mine.
WRITE:/‘Casting from 15==>10 to <any>,”Casting type to typ_mine” in Field-symbol:’, <any>.
ASSIGNlargefieldto<any> CASTINGtypen.
ASSIGNCOMPONENTlw_nameOFSTRUCTURE<lfs_st>to<lfs_field>.
write:/10 lw_name,
30<lfs_field>.
write:/10‘———————————————’.
write:/10‘Using ”Indext” to output’.
在我们实际的ABAP开发中,我们经常会使用动态编程,如动态地获取运行参数,动态创建类型、内表等等。这样做给我们带来的好处之一,就是“扩充”了我们代码的重用性。
一.动态编程(Field Symbol以及casting)
以field symbol为例:
*&———————————————————————*
4.Dynamic Clause Specification
原理:
5.Dynamic Subroutine Specification
原理:
6.总结
测试dynamic field specification,dynamic Type Specification, dynamic Component Specification, Dynamic Clause specification:
原理:
测试程序:
*&———————————————————————*
*& Report ZTEST_DYN_FIELD
*&
*&———————————————————————*
*& test Dynamically field-specification
*&
*&———————————————————————*
lw_nametypestring.“for write
FIELD-SYMBOLS: <lfs_st>typeANY,
<lfs_field>typeany.
PARAMETERS:
p_tab_nmtypestring,
p_condtypestring.
* create the internal-table of Parameter
REPORTZTEST_DYN_FIELD.
*constance
CONSTANTS: atypeivalue1,
btypeivalue2.
* data
DATA:
name(5)TYPEc.
FIELD-SYMBOLS: <lfs>typei.
*——————————–
* test
name =‘A’.
ASSIGN(name)to<lfs>.
30<lfs_field>.
ASSIGNCOMPONENT3OFSTRUCTURE<lfs_st>to<lfs_field>.
write:/10‘Index 3′,
30<lfs_field>.
我们使用数据:
结果:
画面输出:
结果:
ASSIGNlargefieldto<any> CASTINGLIKE<mine>.
WRITE:/‘Casting from 15==>5 to <any>,”Like <mine>” in Field-symbol:’, <any>.
测试程序:
我们的运行结果:
二.动态编程(五类Token分类)
1.Dynamic Field Specification
where(itab_cond).
ENDSELECT.
* write
write:/10‘Using ”Component Name” to output’.
lw_name =‘CARRID’.
ASSIGNCOMPONENTlw_nameOFSTRUCTURE<lfs_st>to<lfs_field>.
TYPES: typ_mine(10)typec.
* define Data type
DATA:
smallfield(5)typec,
largefield(15)typecVALUE‘2345′,
typename(8)typecvalue‘TYP_MINE’,
sometypetypeREFTOCL_ABAP_TYPEDESCR,
ASSIGNCOMPONENT1OFSTRUCTURE<lfs_st>to<lfs_field>.
write:/10‘Index 1′,
30<lfs_field>.
ASSIGNCOMPONENT2OFSTRUCTURE<lfs_st>to<lfs_field>.
write:/10‘Index 2′,
write:/10 lw_name,
30<lfs_field>.
lw_name =‘CONNID’.
ASSIGNCOMPONENTlw_nameOFSTRUCTURE<lfs_st>to<lfs_field>.
write:/10 lw_name,
30<lfs_field>.
lw_name =‘TEXT’.
sometype = cl_abap_typedescr=>DESCRIBE_BY_NAME(‘TYP_MINE’).
“ASSIGN largefield to <any> CASTING TYPE HANDLE sometype.
rrt_type ?= sometype.
ASSIGNlargefieldto<any> CASTINGTYPEHANDLE rrt_type.
CREATEDATAref_datatype(p_tab_nm).
ASSIGNref_data->*to<lfs_st>.
appendp_condtoitab_cond.
* get the data of dynamic table
SELECT*from(p_tab_nm)into<lfs_st>UPTO1ROWS
测试程序:
*&———————————————————————*
*& Report ZTEST_DYN
*&—————————————————————–*
REPORTZTEST_DYN.
DATA:
ref_datatypeREFTOdata,“for creating internal table
itab_condtypeTABLEOFstring,“for condition
WRITE:/‘Casting from 15==>n to <any>,”Casting type to N” in Field-symbol:’, <any>.
ASSIGNlargefieldto<any> CASTINGtype(typename).
WRITE:/‘Casting from 15==>10 to <any>,”Dynamically Casting type to typ_mine” in Field-symbol:’, <any>.
WRITE:/‘Casting from 15==>10 to <any>,”using CL_ABAP_TYPEDESCR” in Field-symbol:’, <any>.
ASSIGNlargefieldto<any> CASTINGLIKEsmallfield.
WRITE:/‘Casting from 15==>5 to <any>,”Like Smallfield” in Field-symbol:’, <any>.
*& Report ZTEST_FIELD_SYMBOL
*& Tang Can
*&———————————————————————*
*& Test Field symbo
*&———————————————————————*
REPORTZTEST_FIELD_SYMBOL.
* define my teype
write:/10 <lfs>.
* test
name =‘B’.
ASSIGN(name)to<lfs>.
write:2.Dynamic Type Specification
原理:
实现:
上面第一点已经总结。
3.Dynamic Component Specification
原理:
rrt_typeTYPEREFTOCL_ABAP_DATADESCR.
FIELD-SYMBOLS: <any>TYPEany, <mine>typetyp_mine.
* type==>type
ASSIGNlargefieldto<mine> CASTING.
WRITE:/‘Casting from 15==>10 to <mine>:’, <mine>.
相关文档
最新文档