2007 Oct 10 7:14 AM
Is constructors concept is there in abap
static constructors?
2007 Oct 10 7:23 AM
Hi,
See this code.
----
CLASS addition DEFINITION
----
*
----
CLASS addition DEFINITION.
PUBLIC SECTION.
METHODS
add IMPORTING num1 TYPE i
num2 TYPE i.
METHODS display.
PRIVATE SECTION.
DATA: total TYPE i.
ENDCLASS. "addition DEFINITION
----
CLASS addition IMPLEMENTATION
----
*
----
CLASS addition IMPLEMENTATION.
METHOD add.
total = num1 + num2.
CALL METHOD display.
ENDMETHOD. "add
METHOD display.
WRITE:/ ' The Sum is ', total.
ENDMETHOD. "display
ENDCLASS. "addition IMPLEMENTATION
PARAMETERS: p_num1 TYPE i,
p_num2 TYPE i.
START-OF-SELECTION.
DATA cref TYPE REF TO addition.
CREATE OBJECT cref.
CALL METHOD cref->add
EXPORTING
num1 = p_num1
num2 = p_num2.
Hi,
See this code.
----
CLASS addition DEFINITION
----
*
----
CLASS addition DEFINITION.
PUBLIC SECTION.
METHODS
add IMPORTING num1 TYPE i
num2 TYPE i.
METHODS display.
PRIVATE SECTION.
DATA: total TYPE i.
ENDCLASS. "addition DEFINITION
----
CLASS addition IMPLEMENTATION
----
*
----
CLASS addition IMPLEMENTATION.
METHOD add.
total = num1 + num2.
CALL METHOD display.
ENDMETHOD. "add
METHOD display.
WRITE:/ ' The Sum is ', total.
ENDMETHOD. "display
ENDCLASS. "addition IMPLEMENTATION
PARAMETERS: p_num1 TYPE i,
p_num2 TYPE i.
START-OF-SELECTION.
DATA cref TYPE REF TO addition.
CREATE OBJECT cref.
CALL METHOD cref->add
EXPORTING
num1 = p_num1
num2 = p_num2.
2007 Oct 10 7:21 AM
Hi
You can use the transaction SE24 to de3fine and implement class. All concepts of OOPs are applicable in ABAP.
You can use the any standard class example to implement your own class.
Thanks,
Mohit
2007 Oct 10 7:23 AM
Hi,
See this code.
----
CLASS addition DEFINITION
----
*
----
CLASS addition DEFINITION.
PUBLIC SECTION.
METHODS
add IMPORTING num1 TYPE i
num2 TYPE i.
METHODS display.
PRIVATE SECTION.
DATA: total TYPE i.
ENDCLASS. "addition DEFINITION
----
CLASS addition IMPLEMENTATION
----
*
----
CLASS addition IMPLEMENTATION.
METHOD add.
total = num1 + num2.
CALL METHOD display.
ENDMETHOD. "add
METHOD display.
WRITE:/ ' The Sum is ', total.
ENDMETHOD. "display
ENDCLASS. "addition IMPLEMENTATION
PARAMETERS: p_num1 TYPE i,
p_num2 TYPE i.
START-OF-SELECTION.
DATA cref TYPE REF TO addition.
CREATE OBJECT cref.
CALL METHOD cref->add
EXPORTING
num1 = p_num1
num2 = p_num2.
2007 Oct 10 7:25 AM
Hi,
Check this example for constructor.
----
CLASS addition DEFINITION
----
*
----
CLASS addition DEFINITION.
PUBLIC SECTION.
METHODS:
constructor.
METHODS:
add IMPORTING num1 TYPE i
num2 TYPE i
EXPORTING total TYPE i.
ENDCLASS. "addition DEFINITION
----
CLASS addition IMPLEMENTATION
----
*
----
CLASS addition IMPLEMENTATION.
METHOD constructor.
WRITE:/ ' Default Constructor or constructor',
'with out any argument called'.
ENDMETHOD. "constructor
METHOD add.
total = num1 + num2.
ENDMETHOD. "add
ENDCLASS. "addition IMPLEMENTATION
PARAMETERS: p_num1 TYPE i,
p_num2 TYPE i.
DATA: l_total TYPE i.
START-OF-SELECTION.
DATA cref TYPE REF TO addition.
CREATE OBJECT cref.
CALL METHOD cref->add
EXPORTING
num1 = p_num1
num2 = p_num2
IMPORTING
total = l_total.
WRITE:/ 'The total obtained using add method',
l_total.
2007 Oct 10 7:25 AM
Hi,
Check this example for constructor.
----
CLASS addition DEFINITION
----
*
----
CLASS addition DEFINITION.
PUBLIC SECTION.
METHODS:
constructor.
METHODS:
add IMPORTING num1 TYPE i
num2 TYPE i
EXPORTING total TYPE i.
ENDCLASS. "addition DEFINITION
----
CLASS addition IMPLEMENTATION
----
*
----
CLASS addition IMPLEMENTATION.
METHOD constructor.
WRITE:/ ' Default Constructor or constructor',
'with out any argument called'.
ENDMETHOD. "constructor
METHOD add.
total = num1 + num2.
ENDMETHOD. "add
ENDCLASS. "addition IMPLEMENTATION
PARAMETERS: p_num1 TYPE i,
p_num2 TYPE i.
DATA: l_total TYPE i.
START-OF-SELECTION.
DATA cref TYPE REF TO addition.
CREATE OBJECT cref.
CALL METHOD cref->add
EXPORTING
num1 = p_num1
num2 = p_num2
IMPORTING
total = l_total.
WRITE:/ 'The total obtained using add method',
l_total.
2007 Oct 10 7:29 AM
Hi Balaji,
<i>Here is the sample code for defining your your own class and implement into the program.
It also includes the event handling....
Please see Below.</i>
*&---------------------------------------------------------------------*
*& Report Z7CC_EVENT_HNDL_EX *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*
report z7cc_oops_class_event_handlng .
class counter definition.
public section.
methods: set importing value(set_value) type i,
increment_counter,
get exporting value(get_value) type i.
events critical_value exporting value(excess) type i.
protected section.
data: count type i.
private section.
data: threshold type i value 10.
endclass.
class handler definition.
public section.
methods handle_excess for event critical_value of counter importing excess.
endclass.
class counter_ten definition inheriting from counter.
public section.
methods increment_counter redefinition .
data count_ten.
endclass.
class counter implementation.
method set.
count = set_value.
endmethod.
method increment_counter.
data diff type i.
add 1 to count.
if count > threshold.
diff = count - threshold.
raise event critical_value
exporting excess = diff.
endif.
endmethod.
method get.
get_value = count.
endmethod.
endclass.
class handler implementation.
method handle_excess.
write: / 'Excess is', excess.
endmethod.
endclass.
class counter_ten implementation.
method increment_counter.
data modulo type i.
call method super->increment_counter.
write: /' Count = ', count.
modulo = count mod 10.
if modulo = 0.
count_ten = count_ten + 1.
write: ' Count_ten = ',count_ten.
endif.
endmethod.
endclass.
data: r1 type ref to counter,
h1 type ref to handler,
number type i value 5.
start-of-selection.
create object r1 type counter_ten.
create object h1.
* CREATE OBJECT: r1, h1.
call method r1->set exporting set_value = number.
set handler h1->handle_excess for all instances.
do 20 times.
call method r1->increment_counter.
enddo.
| User | Count |
|---|---|
| 3 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |