‎2007 Nov 16 1:14 PM
I have a doubt, that meaning has the next code?:
CREATE OBJECT:
account1 EXPORTING id = `...`.
overwrite the constructor of object account1, that have the parameter id, passing the value '...' ?
cordial greetings.
‎2007 Nov 16 3:49 PM
Hi Lopes,
See the follow example.
REPORT z_mar_demo.
*----------------------------------------------------------------------*
* CLASS ONE DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS one DEFINITION.
PUBLIC SECTION.
" The constructor method must be definited on Public Section
" Here we're definig a constructor method without parameters.
METHODS constructor.
PRIVATE SECTION.
DATA attribute TYPE char10.
ENDCLASS. "ONE DEFINITION
*----------------------------------------------------------------------*
* CLASS one IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS one IMPLEMENTATION.
METHOD constructor.
" Inside Construcot we can initialize attributes, call othres methods, etc...
MOVE 'value X' TO attribute.
ENDMETHOD. "CONSTRUCTOR
ENDCLASS. "one IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS two DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS two DEFINITION.
PUBLIC SECTION.
" Here we're definig a constructor method with a parameter.
" Constructor can have only import parameters and its can be defineted
" as Optional
METHODS constructor IMPORTING value TYPE char10 OPTIONAL.
PRIVATE SECTION.
DATA attribute TYPE char10.
ENDCLASS. "two DEFINITION
*----------------------------------------------------------------------*
* CLASS two IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS two IMPLEMENTATION.
METHOD constructor.
IF value IS NOT INITIAL.
MOVE value TO attribute.
ELSE.
MOVE 'value Y' TO attribute.
ENDIF.
ENDMETHOD. "CONSTRUCTOR
ENDCLASS. "two IMPLEMENTATION
DATA o_one TYPE REF TO one.
DATA o_two TYPE REF TO two.
DATA o_two2 TYPE REF TO two.
START-OF-SELECTION.
" As Constructor of class one doesn't has parameters we can create its object as follow
CREATE OBJECT o_one.
" As parameter was definited as Optional we can pass the parameter during object instantiation.
CREATE OBJECT o_two
EXPORTING
value = 'Value Y'.
" Or we can just pass a null value Like value = ' '. or value = SPACE.
CREATE OBJECT o_two
EXPORTING
value = ' '.
" As parameter was definited as Optional we can miss the parameter during object instantiation.
CREATE OBJECT o_two2.
Regards.
Marcelo Ramos
‎2007 Nov 16 2:03 PM
Hello Fernandez
The coding simply means:
- Create an instance account1 and set some default value ( id = '...').Within the CONSTRUCTOR method you can see which instance attribute is filled with the contens of the formal parametr <i>id.</i>
Regards
Uwe
‎2007 Nov 16 3:49 PM
Hi Lopes,
See the follow example.
REPORT z_mar_demo.
*----------------------------------------------------------------------*
* CLASS ONE DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS one DEFINITION.
PUBLIC SECTION.
" The constructor method must be definited on Public Section
" Here we're definig a constructor method without parameters.
METHODS constructor.
PRIVATE SECTION.
DATA attribute TYPE char10.
ENDCLASS. "ONE DEFINITION
*----------------------------------------------------------------------*
* CLASS one IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS one IMPLEMENTATION.
METHOD constructor.
" Inside Construcot we can initialize attributes, call othres methods, etc...
MOVE 'value X' TO attribute.
ENDMETHOD. "CONSTRUCTOR
ENDCLASS. "one IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS two DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS two DEFINITION.
PUBLIC SECTION.
" Here we're definig a constructor method with a parameter.
" Constructor can have only import parameters and its can be defineted
" as Optional
METHODS constructor IMPORTING value TYPE char10 OPTIONAL.
PRIVATE SECTION.
DATA attribute TYPE char10.
ENDCLASS. "two DEFINITION
*----------------------------------------------------------------------*
* CLASS two IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS two IMPLEMENTATION.
METHOD constructor.
IF value IS NOT INITIAL.
MOVE value TO attribute.
ELSE.
MOVE 'value Y' TO attribute.
ENDIF.
ENDMETHOD. "CONSTRUCTOR
ENDCLASS. "two IMPLEMENTATION
DATA o_one TYPE REF TO one.
DATA o_two TYPE REF TO two.
DATA o_two2 TYPE REF TO two.
START-OF-SELECTION.
" As Constructor of class one doesn't has parameters we can create its object as follow
CREATE OBJECT o_one.
" As parameter was definited as Optional we can pass the parameter during object instantiation.
CREATE OBJECT o_two
EXPORTING
value = 'Value Y'.
" Or we can just pass a null value Like value = ' '. or value = SPACE.
CREATE OBJECT o_two
EXPORTING
value = ' '.
" As parameter was definited as Optional we can miss the parameter during object instantiation.
CREATE OBJECT o_two2.
Regards.
Marcelo Ramos
‎2007 Nov 17 5:15 AM
Hi
The CREATE OBJECT statement creates an object in the main memory. The attribute values of this object are either initial values or correspond to the VALUE entry.
Reference variables can also be assigned to each other.
For the above example this would mean that r_vehicle1 and r_vehicle2 point to the same object.