封装了个动态创建对象的类,可以参考ddic对象创建,也可以根据程序内的本地变量创建.
- 使用方法1:根据ddic对象创建
以下代码创建了一个MANTR类型的range表<lt_itab>
,效果等同于lt_itab type range of mara-matnr
,其中参数P_name
为类型名称或对象名称,iv_name
为字段名称
data lr_data type ref to data.
field-symbols:<lt_itab> type any table.
zcl_dynamic_obj=>add_comp_by_name( p_name = 'CHAR1' iv_name = 'SIGN' ).
zcl_dynamic_obj=>add_comp_by_name( p_name = 'CHAR2' iv_name = 'OPTION' ).
zcl_dynamic_obj=>add_comp_by_name( p_name = 'MARA-MATNR' iv_name = 'LOW' ).
zcl_dynamic_obj=>add_comp_by_name( p_name = 'MARA-MATNR' iv_name = 'HIGH' ).
lr_data = zcl_dynamic_obj=>create_table( ).
assign lr_data->* to <lt_itab>.
- 使用方法2:
根据DDIC类型创建,如果iv_as_include
为ABAP_FALSE,则创建的内表为嵌套结构,此时iv_name
为必填,否则会报错.
data lr_data type ref to data.
field-symbols:<lt_itab> type any table.
zcl_dynamic_obj=>add_comp_by_name( p_name = 'MAKT' iv_as_include = abap_true ).
zcl_dynamic_obj=>add_comp_by_name( p_name = 'syst-TABIX' iv_name = 'XH' )."序号
lr_data = zcl_dynamic_obj=>create_table( ).
assign lr_data->* to <lt_itab>.
- 使用方法3
根据程序内的本地对象创建,本地对象包括全局变量和局部变量
data: lv_day type d.
data lr_data type ref to data.
field-symbols:<lt_itab> type any table.
zcl_dynamic_obj=>add_comp_by_data( p_data = lv_day iv_name = 'TODAY' ).
zcl_dynamic_obj=>add_comp_by_data( p_data = lv_day iv_name = 'YESTERDAY' ).
zcl_dynamic_obj=>add_comp_by_data( p_data = lv_day iv_name = 'TOMORROW' ).
lr_data = zcl_dynamic_obj=>create_table( ).
assign lr_data->* to <lt_itab>.
类代码如下,在SE24新建一个类
zcl_dynamic_obj
,然后切换到基于源代码模式,将以下代码粘进去激活即可
class zcl_dynamic_obj definition
public
final
create public .
public section.
type-pools abap .
class-data t_comp type abap_component_tab .
class-methods add_comp_by_table_line
importing
!p_data type any table
!iv_name type c optional
!iv_as_include type abap_bool optional
!iv_suffix type c optional .
class-methods add_comp_by_data
importing
!p_data type any
!iv_name type c optional
!iv_as_include type abap_bool optional
!iv_suffix type c optional .
class-methods add_comp_by_data_ref
importing
!p_data_ref type ref to data
!iv_name type c optional
!iv_as_include type abap_bool optional
!iv_suffix type c optional .
class-methods add_comp_by_name
importing
!p_name type any
!iv_name type c optional
!iv_as_include type abap_bool optional
!iv_suffix type c optional .
class-methods create_struct
returning
value(ro_data) type ref to data .
class-methods create_table
returning
value(ro_data) type ref to data .
protected section.
private section.
endclass.
class zcl_dynamic_obj implementation.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_DYNAMIC_OBJ=>ADD_COMP_BY_DATA
* +-------------------------------------------------------------------------------------------------+
* | [--->] P_DATA TYPE ANY
* | [--->] IV_NAME TYPE C(optional)
* | [--->] IV_AS_INCLUDE TYPE ABAP_BOOL(optional)
* | [--->] IV_SUFFIX TYPE C(optional)
* +--------------------------------------------------------------------------------------</SIGNATURE>
method add_comp_by_data.
data lw_comp like line of t_comp.
lw_comp-type ?= cl_abap_typedescr=>describe_by_data( p_data = p_data ).
lw_comp-name = iv_name.
lw_comp-as_include = iv_as_include.
lw_comp-suffix = iv_suffix.
append lw_comp to t_comp.
endmethod.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_DYNAMIC_OBJ=>ADD_COMP_BY_DATA_REF
* +-------------------------------------------------------------------------------------------------+
* | [--->] P_DATA_REF TYPE REF TO DATA
* | [--->] IV_NAME TYPE C(optional)
* | [--->] IV_AS_INCLUDE TYPE ABAP_BOOL(optional)
* | [--->] IV_SUFFIX TYPE C(optional)
* +--------------------------------------------------------------------------------------</SIGNATURE>
method add_comp_by_data_ref.
data lw_comp like line of t_comp.
lw_comp-type ?= cl_abap_typedescr=>describe_by_data_ref( p_data_ref ).
lw_comp-name = iv_name.
lw_comp-as_include = iv_as_include.
lw_comp-suffix = iv_suffix.
append lw_comp to t_comp.
endmethod.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_DYNAMIC_OBJ=>ADD_COMP_BY_NAME
* +-------------------------------------------------------------------------------------------------+
* | [--->] P_NAME TYPE ANY
* | [--->] IV_NAME TYPE C(optional)
* | [--->] IV_AS_INCLUDE TYPE ABAP_BOOL(optional)
* | [--->] IV_SUFFIX TYPE C(optional)
* +--------------------------------------------------------------------------------------</SIGNATURE>
method add_comp_by_name.
data lw_comp like line of t_comp.
lw_comp-type ?= cl_abap_typedescr=>describe_by_name( p_name = p_name ).
lw_comp-name = iv_name.
lw_comp-as_include = iv_as_include.
lw_comp-suffix = iv_suffix.
append lw_comp to t_comp.
endmethod.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_DYNAMIC_OBJ=>ADD_COMP_BY_TABLE_LINE
* +-------------------------------------------------------------------------------------------------+
* | [--->] P_DATA TYPE ANY TABLE
* | [--->] IV_NAME TYPE C(optional)
* | [--->] IV_AS_INCLUDE TYPE ABAP_BOOL(optional)
* | [--->] IV_SUFFIX TYPE C(optional)
* +--------------------------------------------------------------------------------------</SIGNATURE>
method add_comp_by_table_line.
data lw_comp like line of t_comp.
data lo_wa type ref to data.
field-symbols <lfs_wa> type any.
create data lo_wa like line of p_data.
assign lo_wa->* to <lfs_wa>.
lw_comp-type ?= cl_abap_typedescr=>describe_by_data( p_data = <lfs_wa> ).
lw_comp-name = iv_name.
lw_comp-as_include = iv_as_include.
lw_comp-suffix = iv_suffix.
append lw_comp to t_comp.
endmethod.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_DYNAMIC_OBJ=>CREATE_STRUCT
* +-------------------------------------------------------------------------------------------------+
* | [<-()] RO_DATA TYPE REF TO DATA
* +--------------------------------------------------------------------------------------</SIGNATURE>
method create_struct.
data lr_struct type ref to cl_abap_structdescr.
lr_struct = cl_abap_structdescr=>create( p_components = t_comp ) .
create data ro_data type handle lr_struct.
clear t_comp.
endmethod.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_DYNAMIC_OBJ=>CREATE_TABLE
* +-------------------------------------------------------------------------------------------------+
* | [<-()] RO_DATA TYPE REF TO DATA
* +--------------------------------------------------------------------------------------</SIGNATURE>
method create_table.
data lr_table type ref to cl_abap_tabledescr.
lr_table = cl_abap_tabledescr=>create( cl_abap_structdescr=>create( p_components = t_comp p_strict = '' ) ).
create data ro_data type handle lr_table.
clear t_comp.
endmethod.
endclass.