* Demoklasse für Objekte, welche in einer sortierten Liste verwaltet werden
CLASS lcl_demo DEFINITION.
PUBLIC SECTION.
METHODS:
constructor
IMPORTING
i_name TYPE string.
METHODS:
get_name
RETURNING VALUE(rv_name) TYPE string.
PRIVATE SECTION.
DATA: gv_name TYPE string.
ENDCLASS.
CLASS lcl_demo IMPLEMENTATION.
METHOD constructor.
gv_name = i_name.
ENDMETHOD.
METHOD get_name.
rv_name = gv_name.
ENDMETHOD.
ENDCLASS.
* Listtyp zur Verwaltung der internen Objektreferenzen
* über "name" können die Objektreferenzen gesucht werden
TYPES: BEGIN OF ty_instances,
name TYPE string,
o_instance TYPE REF TO lcl_demo,
END OF ty_instances.
* sortierte Liste zur Verwaltung der Objektreferenzen, Primärschlüssel ist "name"
DATA: it_instances TYPE SORTED TABLE OF ty_instances WITH UNIQUE KEY name.
START-OF-SELECTION.
* Objekte generieren und in die iTab einfügen
* iTab wird automatisch anhand der Spalte "name" sortiert
INSERT VALUE #( name = 'REF2'
o_instance = NEW #( 'Udo' ) ) INTO TABLE it_instances.
INSERT VALUE #( name = 'REF1'
o_instance = NEW #( 'Heinz' ) ) INTO TABLE it_instances.
INSERT VALUE #( name = 'REF4'
o_instance = NEW #( 'Horst' ) ) INTO TABLE it_instances.
INSERT VALUE #( name = 'REF3'
o_instance = NEW #( 'Rainer' ) ) INTO TABLE it_instances.
* Objekt anhand eines Namens suchen
DATA(o_ref) = it_instances[ name = 'REF1' ]-o_instance.
WRITE: / o_ref->get_name( ).
* sortierte Objektliste ausgeben
LOOP AT it_instances ASSIGNING FIELD-SYMBOL(<fs_inst>).
WRITE: / <fs_inst>-name, <fs_inst>-o_instance->get_name( ).
ENDLOOP.