ABAP--动态创建局部类型的变量
2008-11-19 12:48
477 查看
REPORT ZDANY_DYN_LOCAL_TYPES.
****************** hardcoded "old style" local type*******************
* This is a normal hardcoded local type
types : begin of typ_hardcoded,
l_count type i,
lt_sflight type sflight.
types : end of typ_hardcoded.
* create a table based on hardcoded type
data : lt_hardcoded type table of typ_hardcoded.
****************** dynamic "new wave" local type *******************
types: typ_count type i.
field-symbols : <lt_dynamic> type any table.
data: dref type ref to data,
itab_type type ref to cl_abap_tabledescr,
struct_type type ref to cl_abap_structdescr,
elem_type type ref to cl_abap_elemdescr,
comp_tab type cl_abap_structdescr=>component_table,
comp_fld type cl_abap_structdescr=>component.
* We read information about each fields of SFLIGHT (see ABAP FAQ #2)
struct_type ?= cl_abap_typedescr=>describe_by_name( 'SFLIGHT' ).
* We also need the information about the type "typ_count", note that
* we should use class cl_abap_elemdescr instead of cl_abap_typedescr
elem_type ?= cl_abap_elemdescr=>describe_by_name( 'TYP_COUNT' ).
* we read all fleids of SFLIGHT and create a component table
comp_tab = struct_type->get_components( ).
* We add manually the counter
comp_fld-name = 'L_COUNT'.
comp_fld-type = elem_type.
insert comp_fld into comp_tab index 1.
* we create the structure
struct_type = cl_abap_structdescr=>create( comp_tab ).
* ... and the internal table
itab_type = cl_abap_tabledescr=>create( struct_type ).
* The new thing here is the "type handle" which create a pointer to a handle
create data dref type handle itab_type.
* we finally assign a field symbol to the pointer because we cannot directly access a pointer.
assign dref->* to <lt_dynamic>.
* At the end of this small program, internal table lt_hardcoded and lt_dynamic are the same
****************** hardcoded "old style" local type*******************
* This is a normal hardcoded local type
types : begin of typ_hardcoded,
l_count type i,
lt_sflight type sflight.
types : end of typ_hardcoded.
* create a table based on hardcoded type
data : lt_hardcoded type table of typ_hardcoded.
****************** dynamic "new wave" local type *******************
types: typ_count type i.
field-symbols : <lt_dynamic> type any table.
data: dref type ref to data,
itab_type type ref to cl_abap_tabledescr,
struct_type type ref to cl_abap_structdescr,
elem_type type ref to cl_abap_elemdescr,
comp_tab type cl_abap_structdescr=>component_table,
comp_fld type cl_abap_structdescr=>component.
* We read information about each fields of SFLIGHT (see ABAP FAQ #2)
struct_type ?= cl_abap_typedescr=>describe_by_name( 'SFLIGHT' ).
* We also need the information about the type "typ_count", note that
* we should use class cl_abap_elemdescr instead of cl_abap_typedescr
elem_type ?= cl_abap_elemdescr=>describe_by_name( 'TYP_COUNT' ).
* we read all fleids of SFLIGHT and create a component table
comp_tab = struct_type->get_components( ).
* We add manually the counter
comp_fld-name = 'L_COUNT'.
comp_fld-type = elem_type.
insert comp_fld into comp_tab index 1.
* we create the structure
struct_type = cl_abap_structdescr=>create( comp_tab ).
* ... and the internal table
itab_type = cl_abap_tabledescr=>create( struct_type ).
* The new thing here is the "type handle" which create a pointer to a handle
create data dref type handle itab_type.
* we finally assign a field symbol to the pointer because we cannot directly access a pointer.
assign dref->* to <lt_dynamic>.
* At the end of this small program, internal table lt_hardcoded and lt_dynamic are the same
相关文章推荐
- ABAP--动态创建局部类型的变量
- ABAP--动态创建局部类型的变量
- ABAP--动态创建局部类型的变量
- ABAP--动态创建局部类型的变量
- ABAP--动态创建类型和变量的使用程序样例
- ABAP--动态创建类型和变量的使用程序样例
- ABAP--动态创建类型和变量的使用程序样例
- ABAP--动态创建类型和变量的使用程序样例
- 根据Type 变量动态创建可变内部类型的泛型
- MFC技术内幕系列之(三)----MFC执行期类型识别与动态创建技术内幕
- C# 通过反射来动态创建泛型类型
- python创建及引用动态变量名的方法
- 动态类型识别&动态创建
- iOS动态特性初研究(利用JSON动态创建类型和对象)
- php使用变量动态创建类的对象用法示例
- Runtime 创建动态一个类 添加成员变量 属性
- C# 中反射获取某类的子类和根据类型名动态创建对象(转载)
- RTTI运行时类型识别与类对象的动态创建
- .Net 中的反射(动态创建类型实例) - Part.4(转自http://www.tracefact.net/CLR-and-Framework/Reflection-Part4.aspx)
- 内存分配方式(动态/静态)的不同对const变量(全局/局部)访问性的影响