ABAP--动态创建局部类型的变量

本文介绍如何使用ABAP创建硬编码本地类型和动态本地类型。通过硬编码方式定义了一个简单的本地类型,并创建了相应的表。随后,利用动态方式读取SFLIGHT结构信息并手动添加计数字段,最终创建出等效的动态结构及内部表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值