Objects must be created at runtime (using CREATE OBJECT). With their creation they also get their own identity. However, there are no fixed attribute values linked to the identity. You are probably already wondering how objects get to their initial state. How do objects recognize their initial attribute values?
TheConstructor concept exists specifically to answer this question. The constructor is a method which runs automatically during the creation of an object. The constructor allows you to define IMPORTING-parameters.
In ABAP Objects you differentiate between instance-dependent and class-dependent constructors via the language elements METHODS{{ }}and CLASS-METHODS to be used in the definition part and via their namesconstructor and CLASS_CONSTRUCTOR:
In ABAP Objects you differentiate between instance-dependent and class-dependent constructors via the language elements METHODS{{ }}and CLASS-METHODS to be used in the definition part and via their namesconstructor and CLASS_CONSTRUCTOR:
The class constructor is called by the first access to a class element (method, attribute, event, and object), the (instance) constructor by the creation of an object (CREATE OBJECT).
REPORT Z_003_CONSTRUCTOR.
TYPES: TY_NAME(50) TYPE C.
*----------------------------------------------------------------
* CLASS CL_NAME DEFINITION
*----------------------------------------------------------------
CLASS CL_NAME DEFINITION.
PUBLIC SECTION.
CLASS-DATA I_COUNT TYPE I. " Class Dependent Attribute
" Will have same value in all objects
DATA NAME TYPE TY_NAME. " Instance Dependent Attribute
" Will have different values in different objects
CLASS-METHODS CLASS_CONSTRUCTOR.
METHODS: CONSTRUCTOR IMPORTING VALUE(IM_NAME) TYPE TY_NAME,
GET_NAME EXPORTING VALUE(EX_NAME) TYPE TY_NAME.
ENDCLASS. "CL_NAME DEFINITION
*----------------------------------------------------------------
* CLASS CL_NAME IMPLEMENTATION
*----------------------------------------------------------------
CLASS CL_NAME IMPLEMENTATION.
METHOD CLASS_CONSTRUCTOR.
I_COUNT = I_COUNT + 10.
ENDMETHOD. "IS_CLASS_INITIATED
METHOD CONSTRUCTOR.
NAME = IM_NAME.
ENDMETHOD. "SET_NAME
METHOD GET_NAME.
EX_NAME = NAME.
ENDMETHOD. "GET_NAME
ENDCLASS. "CL_NAME IMPLEMENTATION
START-OF-SELECTION.
" CREATE INSTANCE OF THIS CLASS, IF NO INSTANCE EXISTS.
DATA: INSTANCE1 TYPE REF TO CL_NAME,
INSTANCE2 TYPE REF TO CL_NAME,
NAME1 TYPE TY_NAME,
NAME2 TYPE TY_NAME.
CREATE OBJECT INSTANCE1
EXPORTING
IM_NAME = 'VINOTH'.
CREATE OBJECT INSTANCE2
EXPORTING
IM_NAME = 'BALAJI'.
" GET VALUE OF NAME ATTRIBUTE
CALL METHOD INSTANCE1->GET_NAME
IMPORTING
EX_NAME = NAME1.
CALL METHOD INSTANCE2->GET_NAME
IMPORTING
EX_NAME = NAME2.
" DISPLAY ATTRIBUTES OF INSTANCE1
WRITE: 'ATTRIBUTES OF INSTANCE 1 : ',
/ 'COUNT : ', INSTANCE1->I_COUNT,
/ 'NAME : ' , NAME1.
SKIP.
SKIP.
" DISPLAY ATTRIBUTES OF INSTANCE2
WRITE: 'ATTRIBUTES OF INSTANCE 2 : ',
/ 'COUNT : ', INSTANCE2->I_COUNT,
/ 'NAME : ' , NAME2.
" CLEAR MEMORY OCCUPIED BY OBJECTS
CLEAR: INSTANCE1,
INSTANCE2.
Program Output : 003
ATTRIBUTES OF INSTANCE 1 :
COUNT : 10
NAME : VINOTH
ATTRIBUTES OF INSTANCE 2 :
COUNT : 10
NAME : BALAJI
No comments:
Post a Comment