loveplxf

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

    在我们实际的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: <any>  TYPE any, <mine>  type typ_mine.

* type==>type
  ASSIGN largefield to <mine> CASTING.
  WRITE:/ ‘Casting from 15==>10 to <mine>:’, <mine>.

  ASSIGN largefield to <any> CASTING type typ_mine.
  WRITE:/ ‘Casting from 15==>10 to <any>,”Casting type to typ_mine” in Field-symbol:’, <any>.

  ASSIGN largefield to <any> CASTING type n.
  WRITE:/ ‘Casting from 15==>n to <any>,”Casting type to N” in Field-symbol:’, <any>.

  ASSIGN largefield to <any> CASTING type (typename).
  WRITE:/ ‘Casting from 15==>10 to <any>,”Dynamically Casting type to typ_mine” in Field-symbol:’, <any>.

  sometype = cl_abap_typedescr=>DESCRIBE_BY_NAME( ‘TYP_MINE’ ).
  “ASSIGN largefield to <any> CASTING TYPE HANDLE sometype.
  rrt_type ?= sometype.
  ASSIGN largefield to <any> CASTING TYPE HANDLE rrt_type.
  WRITE:/ ‘Casting from 15==>10 to <any>,”using CL_ABAP_TYPEDESCR” in Field-symbol:’, <any>.

  ASSIGN largefield to <any> CASTING LIKE smallfield.
  WRITE:/ ‘Casting from 15==>5 to <any>,”Like Smallfield” in Field-symbol:’, <any>.

  ASSIGN largefield to <any> CASTING LIKE <mine>.
  WRITE:/ ‘Casting from 15==>5 to <any>,”Like <mine>” in Field-symbol:’, <any>.
测试程序,我们的运行结果:

二.动态编程(五类Token分类)

1.Dynamic Field Specification

原理:

测试程序

*&———————————————————————*
*& 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: <lfs>  type i.
* test
 name = ‘A’.
 ASSIGN (name) to <lfs>.
 write:/10 <lfs>.
* test
 name = ‘B’.
 ASSIGN (name) to <lfs>.
 write:/10 <lfs>.

结果:

2.Dynamic Type Specification

原理:

实现:

上面第一点已经总结。

 

3.Dynamic Component Specification

原理:

 4.Dynamic Clause Specification

原理:

 

5.Dynamic Subroutine Specification

原理:

 

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: <lfs_st>  type ANY,
               <lfs_field>  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 <lfs_st>.
  append p_cond to itab_cond.
* get the data of dynamic table
  SELECT * from (p_tab_nm) into <lfs_st> 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 <lfs_st> to <lfs_field>.
  write:/10 lw_name,
             30 <lfs_field>.
  lw_name = ‘CONNID’.
  ASSIGN COMPONENT lw_name OF STRUCTURE <lfs_st> to <lfs_field>.
  write:/10 lw_name,
             30 <lfs_field>.
  lw_name = ‘TEXT’.
  ASSIGN COMPONENT lw_name OF STRUCTURE <lfs_st> to <lfs_field>.
  write:/10 lw_name,
             30 <lfs_field>.
  write:/10 ‘———————————————’.
  write:/10 ‘Using ”Indext” to output’.
  ASSIGN COMPONENT 1 OF STRUCTURE <lfs_st> to <lfs_field>.
  write:/10 ‘Index 1′,
             30 <lfs_field>.
  ASSIGN COMPONENT 2 OF STRUCTURE <lfs_st> to <lfs_field>.
  write:/10 ‘Index 2′,
             30 <lfs_field>.
  ASSIGN COMPONENT 3 OF STRUCTURE <lfs_st> to <lfs_field>.
  write:/10 ‘Index 3′,
             30 <lfs_field>.
我们使用数据: 
 
结果:
画面输出  
结果 
719
————-未完待续——————–
 
 
文中用到的ppt文档(ABAP动态编程--官方资料),下载地址:
posted on 2012-08-15 21:18  loveplxf  阅读(433)  评论(0编辑  收藏  举报