✒️ABAP El tratamiento de los Objetos
ABAP El tratamiento de los Objetos
Object Handling
Object Utilization
Objects are instances of classes. Each object has its own identity and attributes. Objects are transient and exist within the context of a session. This means they are being used in the program, have a reference pointing to them, or have at least one event handler method registered in the object. The number of objects generated by a class is unlimited.
Object References
These are pointers to ABAP objects used to access an object from an ABAP program. ABAP objects are always contained in referenced variables. These variables contain either the initial value or a reference to an existing object. Direct access to an object is not possible.
Referenced variables are treated like any elementary data object, so they can contain internal tables or structures.
ABAP has a predefined data type for references. The complete data type is not defined until the declaration in the ABAP program. Depending on the data type of the referenced variable, the program determines how to act with the object reference or its value.
There are two main types of references:
- Class reference: Defined using the clause TYPE REF TO <class name>. This clause applies to TYPES or DATA statements. These references allow the user to create an object of the class and access visible components using the statement cref --> component, where cref is the reference variable to the component.
DATA: cnt TYPE REF TO contador,
sum TYPE REF TO sumador.
- Interface reference: To be covered later.
Object Creation
To create an object of a class, you first need to declare a referenced variable with a reference to the class. Once this is done, you can create the object using the statement CREATE OBJECT <referenced variable>. After the execution of the statement, the referenced variable will hold the object reference.
If there is no reference pointing to the object, it cannot be accessed syntactically, so the garbage collector will destroy all unreferenced objects when activated automatically next time, freeing up the memory space occupied by the affected object or objects. The garbage collector ensures the use of resources blocked by elements that no longer have any reason to exist in the system. If we want to know if the object is referenced or not, we can use the following if statement: If <reference variable name> is initial is true implies that the object assigned to the reference variable is not in use.
* Creo las instancias de los objetos
CREATE OBJECT cnt.
CREATE OBJECT sum.
How to Access Object Components?
Programs only access instance components using referenced variable references. Considering ref as the referenced variable:
- Access to an attribute: ref --> attr.
- Method call: CALL METHOD ref --> meth.
CALL METHOD sum->set
EXPORTING
set_value = number2.
For access to static or instance-independent components, only class-dependent ones can be accessed by both the class name and the referenced variable. It's possible to access static components of a class before an object of the same class has been created. Assuming class is a class:
- Access to a static attribute: class --> attr.
- Access to a static method: CALL METHOD class --> meth.
Additionally, it's possible to access individual components of a class from the class itself using self-reference ME:
- Access to an attribute of the class itself: me --> attr.
- Access to a method of the class itself: CALL METHOD me --> meth.
me->carrier_id = i_carrier_id.
me->connection_id = i_connection_id.
Constructors are a special type of methods that cannot be called using CALL METHOD. They are automatically called to establish the initial state of a new object or class. There are two types of constructors:
- Instance-dependent.
- Instance-independent or static.
They are methods with a predefined name and must be explicitly declared in the class. A constructor method is required if after creating an instance of a class, we need to assign resources, initialize attributes in a way not possible for the VALUE clause of the DATA statement, modify static attributes, or send messages indicating the creation of a new object. There are no destructor methods in ABAP objects.
Reference Assignment
It's possible to assign references to different referenced variables using the MOVE statement. This way, multiple differentiated variables can point to the same object.
When assigning a reference to a different referenced variable, their types must be compatible. Whether using the MOVE statement or direct assignment (=), the system must determine in syntax checking that the assignment will be possible. The same type of validation applies when passing referenced variables as parameters to procedures.
If we write the statement cref1 = cref2, both references must have the same type. That is, both references refer to the same class, or the class of cref1 must be a predefined class like empty or OBJECT.
The object class is a class that has no components and serves the same function for referenced variables as the ANY data type for normal variables. Referenced variables with the OBJECT type can function as containers for passing references but can never be used to access objects.
E.g
*----------------------------------------------------------------------*
* CLASS contador DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS contador DEFINITION.
PUBLIC SECTION.
METHODS: set IMPORTING VALUE(set_value) TYPE i,
incrementar_contador,
get EXPORTING VALUE(get_value) TYPE i.
PRIVATE SECTION.
DATA count TYPE i.
ENDCLASS. "contador DEFINITION
*----------------------------------------------------------------------*
* CLASS contador IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS contador IMPLEMENTATION.
METHOD set.
count = set_value.
ENDMETHOD. "set
METHOD incrementar_contador.
ADD 1 TO count.
ENDMETHOD. "incrementar_contador
METHOD get.
get_value = count.
ENDMETHOD. "get
ENDCLASS. "contador IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS sumador DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS sumador DEFINITION.
PUBLIC SECTION.
METHODS: set IMPORTING VALUE(set_value) TYPE i,
incrementar_sumador,
get EXPORTING VALUE(get_value) TYPE i.
PRIVATE SECTION.
DATA sum TYPE i.
ENDCLASS. "sumador DEFINITION
*----------------------------------------------------------------------*
* CLASS sumador IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS sumador IMPLEMENTATION.
METHOD set.
sum = set_value.
ENDMETHOD. "set
METHOD incrementar_sumador.
ADD 100 TO sum.
ENDMETHOD. "incrementar_sumador
METHOD get.
get_value = sum.
ENDMETHOD. "get
ENDCLASS. " sumador IMPLEMENTATION
*----------------------------------------------------------------------*
* Declaración de variables
*----------------------------------------------------------------------*
DATA: number TYPE i VALUE 5,
number2 TYPE i VALUE 50.
DATA: cnt TYPE REF TO contador,
sum TYPE REF TO sumador.
*----------------------------------------------------------------------*
* Comienza el procesamiento
*----------------------------------------------------------------------*
START-OF-SELECTION.
* Creo las instancias de los objetos
CREATE OBJECT cnt.
CREATE OBJECT sum.
CALL METHOD cnt->set
EXPORTING
set_value = number.
CALL METHOD sum->set
EXPORTING
set_value = number2.
DO 3 TIMES.
CALL METHOD cnt->incrementar_contador.
ENDDO.
CALL METHOD sum->incrementar_sumador.
* Recupero los valores y los imprimo por pantalla
CALL METHOD cnt->get
IMPORTING
get_value = number.
WRITE number.
CALL METHOD sum->get
IMPORTING
get_value = number2.
WRITE number2.
 
 
 
Sobre el autor
Publicación académica de Jaime Eduardo Gomez Arango, en su ámbito de estudios para la Carrera Consultor ABAP.
Jaime Eduardo Gomez Arango
Profesión: Ingeniero de Sistemas y Computación - España - Legajo: SW34C
✒️Autor de: 118 Publicaciones Académicas
🎓Cursando Actualmente: Consultor ABAP Nivel Avanzado
🎓Egresado del módulo:
Disponibilidad Laboral: FullTime
Presentación:
Ingeniero de sistemas y computación con 8 años de experiencia el desarrollo frontend & backend (react/node) y en cloud (aws), actualmente desarrollando habilidades en sap btp, ui5, abap y fiori.
Certificación Académica de Jaime Gomez