ABAP Help Document(12):5.数据对象创建

5.Creating Objects

    动态创建对象,创建数据对象或类对象。

5.1 create data

创建数据对象:CREATE DATA;

示例:

"动态对象
"CREATE DATA
"隐式定义数据类型
"1. CREATE DATA dref [area_handle].
"使用ABAP预定义类型
*2. CREATE DATA dref [area_handle]
*     TYPE {abap_type|(name)}
*     [LENGTH len] [DECIMALS dec].
"d,t,i,f,decfloat16,decfloat34,string,xstring
"不需要指定length,decimals
"c,n,x
"需要指定length
"p
"需要指定length,decimals
"使用已有Type
*3. CREATE DATA dref [area_handle]
*   { {TYPE [LINE OF] {type|(name)}}
*   | {LIKE [LINE OF] dobj}  }.
"引用类型创建变量
*4. CREATE DATA dref [area_handle]
*                   TYPE HANDLE handle.
"创建reference变量
*5. CREATE DATA dref [area_handle]
*    TYPE REF TO {type|(name)}.
"创建内表
*6. CREATE DATA dref [area_handle]
*{ {TYPE [STANDARD]|SORTED|HASHED TABLE OF [REF TO] {type|(name)}}
* | {LIKE [STANDARD]|SORTED|HASHED TABLE OF dobj} }
* [ {WITH [UNIQUE|NON-UNIQUE]
* {KEY {comp1 comp2 ...}|(keytab)}|{DEFAULT KEY}}
* | {WITH EMPTY KEY} ]
* [INITIAL SIZE n].
FORM f_create_obj.
  TYPES:t_itab TYPE TABLE OF i WITH NON-UNIQUE KEY table_line.
  DATA:lt_itab_ref TYPE REF TO t_itab.
  DATA:i_ref TYPE REF TO i.
  DO 10 TIMES.
    IF lt_itab_ref IS INITIAL.
      CREATE DATA lt_itab_ref.
    ENDIF.
    APPEND sy-index TO lt_itab_ref->*.
  ENDDO.
  IF i_ref IS INITIAL.
    CREATE DATA i_ref.
  ENDIF.
  LOOP AT lt_itab_ref->* INTO i_ref->*.
    WRITE:/ i_ref->*.
  ENDLOOP.

  "使用ABAP已有类型
  DATA:lv_ref TYPE REF TO DATA.
  CREATE DATA lv_ref TYPE P LENGTH 8 DECIMALS 2.

  "使用type定义结构体,或数据库表结构
  DATA:lt_ref TYPE REF TO DATA.
  FIELD-SYMBOLS:<fs_wa> TYPE ANY.
  FIELD-SYMBOLS:<fs_comp> TYPE ANY.
  "创建数据
  CREATE DATA lt_ref TYPE spfli.
  ASSIGN lt_ref->* TO <fs_wa>.
  SELECT * FROM spfli UP TO 1 ROWS INTO <fs_wa>.
    "读取前四列
    DO 4 TIMES.
      ASSIGN COMPONENT sy-index OF STRUCTURE <fs_wa> TO <fs_comp>.
      IF sy-subrc = 0.
        WRITE:/ <fs_comp>.
      ELSE.
        EXIT.
      ENDIF.
    ENDDO.
  ENDSELECT.

  "动态数据类型
  "CL_ABAP_DATADESCR
  "CL_ABAP_ELEMDESCR
  "element,i,c,n,string,decfoat等类型
  "CL_ABAP_STRUCTDESCR, CL_ABAP_TABLEDESCR,CL_ABAP_REFDESCR
  "structure,table,reference类型
  DATA:lo_struct TYPE REF TO cl_abap_structdescr.
  DATA:lt_comp_tab TYPE cl_abap_structdescr=>component_table.
  DATA:ls_comp_tab LIKE LINE OF lt_comp_tab.
  DATA:lv_ref1 TYPE REF TO DATA.
  "get方法exception,CX_SY_STRUCT_CREATION
  DATA:lo_excep TYPE REF TO cx_sy_struct_creation.
  FIELD-SYMBOLS:<fs_struct> TYPE ANY.
  FIELD-SYMBOLS:<fs_comp1> TYPE ANY.
  FIELD-SYMBOLS:<fs_comp2> TYPE ANY.
  "构造结构
  ls_comp_tab-name = 'name'.
  ls_comp_tab-type = cl_abap_elemdescr=>get_string( ).
  APPEND ls_comp_tab TO lt_comp_tab.
  ls_comp_tab-name = 'age'.
  ls_comp_tab-type = cl_abap_elemdescr=>get_i( ).
  APPEND ls_comp_tab TO lt_comp_tab.
  "获取数据类型
  TRY .
    lo_struct = cl_abap_structdescr=>get( lt_comp_tab ).
    "根据动态结构创建数据
    CREATE DATA lv_ref1 TYPE HANDLE lo_struct.
    "数据
    ASSIGN lv_ref1->* TO <fs_struct>.
    ASSIGN COMPONENT 'NAME' OF STRUCTURE <fs_struct> TO <fs_comp1>.
    <fs_comp1> = 'Tom'.
    ASSIGN COMPONENT 'AGE' OF STRUCTURE <fs_struct> TO <fs_comp2>.
    <fs_comp2> = 10.
  CATCH cx_sy_struct_creation INTO lo_excep.
    MESSAGE lo_excep TYPE 'E'.
    RETURN.
  ENDTRY.

  "根据类名
  "CL_ABAP_STRUCTDESCR
*  DATA:lv_ref2 TYPE REF TO DATA.
*  DATA:lv_cls_name TYPE string.
*  FIELD-SYMBOLS:<fs_cls> TYPE ANY.
*  FIELD-SYMBOLS:<fs_attr> TYPE ANY.
*  DATA:lo_oo_cls TYPE REF TO cl_oo_class.
*  TRY .
*    "获取类名
*    CREATE OBJECT lo_oo_cls EXPORTING clsname = ''.
*    lv_cls_name = lo_oo_cls->class-clsname.
*  CATCH cx_class_not_existent.
*  ENDTRY.
*  lv_cls_name = '\PROGRAM=program名\CLASS=类名'.
*  CREATE DATA lv_ref2 TYPE (lv_cls_name).
*  ASSIGN lv_ref2->* TO <fs_cls>.
*  ASSIGN ('<FS_CLS>->类attr名') TO <fs_attr>.
*  WRITE:/ <fs_attr>.

ENDFORM

5.2 create object

创建类对象:CREATE OBJECT;

示例2:

"CREATE OBJECT
"隐式定义类
*1. CREATE OBJECT oref [area_handle] [parameter_list].
"明确定义类
*2. CREATE OBJECT oref [area_handle]
*  TYPE { class  [parameter_list] }
*  | { (name) [parameter_list|parameter_tables] }.
"创建object,有参构造
*[EXPORTING  p1 = a1 p2 = a2 ...]
*[EXCEPTIONS exc1 = n1 exc2 = n2 ... [OTHERS = n_others]].
*[PARAMETER-TABLE ptab]
*[EXCEPTION-TABLE etab].
FORM f_create_obj1.
  DATA: container TYPE REF TO cl_gui_container,
        exc_ref TYPE REF TO cx_root,
        exc_text TYPE string.

  DATA: class TYPE string VALUE `CL_GUI_DIALOGBOX_CONTAINER`,
        ptab TYPE abap_parmbind_tab,
        ptab_line TYPE abap_parmbind.

  ptab_line-name = 'PARENT'.
  ptab_line-kind = cl_abap_objectdescr=>exporting.
  GET REFERENCE OF CL_GUI_CONTAINER=>DESKTOP
                INTO ptab_line-value.
  INSERT ptab_line INTO TABLE ptab.

  ptab_line-name = 'WIDTH'.
  ptab_line-kind = cl_abap_objectdescr=>exporting.
  GET REFERENCE OF 1000 INTO ptab_line-value.
  INSERT ptab_line INTO TABLE ptab.

  ptab_line-name = 'HEIGHT'.
  ptab_line-kind = cl_abap_objectdescr=>exporting.
  GET REFERENCE OF 300 INTO ptab_line-value.
  INSERT ptab_line INTO TABLE ptab.

  TRY.
      CREATE OBJECT container TYPE (class)
                    PARAMETER-TABLE ptab.
    CATCH cx_sy_create_object_error INTO exc_ref.
      exc_text = exc_ref->get_text( ).
      MESSAGE exc_text TYPE 'I'.
  ENDTRY.
ENDFORM.

5.3 shared objects

    Shared Objects:创建Object作为Shared Objects,定义类时使用语法:SHARED MEMORY ENABLED;

    Area Class:定义Area,使用Tcode:SHMA。全局终结Area类:CL_SHM_AREA;所有Area类父类:CL_ABAP_MEMORY_AREA;

Areas

Areas-Basic Properties:

1. Area Name,示例:'CL_';2. Area Root Class, 是全局定义内存共享的Class;3. Client-Dependent Area,使用Client区分Area;4. Automatic Area Building;5. Transactional Area;

Areas-Fixed Area Properties:

1. Area Binding;2. Area Instance Versioning;

Area-Dynamic Area Properties:

1.Area Constructor Class,实现SHM_IF_SHM_BUILD_INSTANCE接口类,IF_SHM_BUILD_INSTANCE~BUILD类方法作为constructor方法;

2. Displacement Type,可能的displacement type:Displacement Not Possible, Displacement Possible。

Areas-Runtime-Dependent Area Properties:

1. Area Build Type,可能值:No Autostart、Autostart for Read Request、Autostart for Read Request and Every Invalidation;

2. Size of an Area Instance Version,可能值:No limit、Maximum ... KB;

    3.Number of Versions of an Area Instance,可能值:No limit、Maximum ...;

    4.Lifetime of an Area Instance,可能值: No specification、Expires after ... minutes、Refresh after ... minutes、Without read access ... minutes;

 

Area Instance Versions

    管理存储Shared Objects。Tcode: SHMM,overview of area instance。类:CL_SHM_UTILITIES;

Area Handles:

    Area Handles是area class的实例;

    Area Lock, Creating Area Instance Versions, ATTACH_FOR_WRITE;Changing Area Instance Versions,ATTACH_FOR_UPDATE;Reading Area Instance Versions,ATTACH_FOR_READ;

    Deactivating Area Handles, 移除Reading Lock,DETACH; 移除Changing Lock,并提交修改,DETACH_COMMIT; 移除Changing Lock,并回滚修改,DETACH_ROLLBACK;DETACH_AREA,DETACH_ALL_AREAS,一次移除多个绑定;

    Additional Methods,1. Methods INVALIDATE_INSTANCE or INVALIDATE_AREA;

2. Methods FREE_INSTANCE or FREE_AREA;3. Method GET_INSTANCE_INFOS;4. Methods GET_LOCK_KIND, IS_VALID, IS_ACTIVE_VERSION, IS_SHARED, HAS_ACTIVE_PROPERTIES and GET_DETACH_INFO,Area handle信息;5 .BUILD method,调用area constructor方法;

    Area Instance Versions状态:1.Building;2.Active;3.Obsolete;4. Expired;

 

Shared Objects

    Access Shared Objects,使用GET_HANDLE_BY_OREF, GET_HANDLE_BY_DREF, and GET_HANDLE_BY_DATA获取CL_ABAP_MEMORY_AREA类实例;

    Shared Objects创建,CREATE OBJECT oref AREA HANDLE handle;

CREATE DATA dref AREA HANDLE handle;

Area类结构:

CL_ABAP_MEMORY_AREA

  --CL_SHM_AREA

--Area Classes

  --CL_IMODE_AREA

CL_ABAP_MEMORY_AREA is the superclass of all area classes.

CL_SHM_AREA is the superclass of all self-defined area classes.

CL_IMODE_AREA is a predefined area class for the internal session.

Area接口:

IF_SHM_BUILD_INSTANCE

--Area Constructor Class

 

CL_ABAP_MEMORY_AREA类结构:

1.Static Methods

GET_HANDLE_BY_OREF

GET_HANDLE_BY_DREF

GET_HANDLE_BY_DATA

2.Instance Methods

IS_SHARED

IS_VALID

IS_ACTIVE_VERSION

HAS_ACTIVE_PROPERTIES

GET_DETACH_INFO

 

CL_SHM_AREA类结构:

1.Instance Attributes

PROPERTIES

INST_NAME

CLIENT

Static Methods

MULTI_ATTACH

DETACH_ALL_AREAS

2.Instance Methods

DETACH

DETACH_COMMIT

DETACH_ROLLBACK

GET_LOCK_KIND

GET_ROOT

3.Instance Events

SHM_COMMIT_EVENT

SHM_ROLLBACK_EVENT

示例:DEMO_SHARED_OBJECTS

posted @ 2021-04-22 19:09  渔歌晚唱  阅读(327)  评论(0编辑  收藏  举报