2006 Jul 05 4:48 AM
Would someone give a very simple example how to create a Class that Adds two numbers?
Let me know the Transaction also to build the class.
Thanks.
2006 Jul 05 4:53 AM
2006 Jul 05 4:53 AM
2006 Jul 05 5:26 AM
Hi Rohini,
You can use class in two ways.
1) Global Class
It can be created in se24.Give the name of the class and short description and you will get into the class builder
In the attributes tab, give the two numbers as
a Public Instance Type I description
b Public Instance Type I description
In the methods tab, Click on the constructor button to create the constructor method. You can click on the parameters tab to give the parameters for the method
Give the parameters as
param_a Importing Type I description
param_b Importing Type I description
Now create a new method whose description is
add Instance Description
Double click on the method name to go into the editor.
In the constructor method
Just assign
a = param_a and
b = param_b
Now in the add method
data: sum type i.
sum = a + b.
message i001(zmess) with sum.
Now save and activate the class.
Now in the main program.
data: obj type ref to ZMYCLASS.
start-of-selection.
create object obj exporting param_a = 10
param_b = 20.
call method obj->add.
This will be the way to use classes.
2) Local classes.
Just go through this document to have a better idea.
http://esnips.com/doc/5c65b0dd-eddf-4512-8e32-ecd26735f0f2/prefinalppt.ppt
<b>Close the thread once the problem is resolved.</b>
Regards,
SP.
2006 Jul 05 5:28 AM
Hi,
Use he transaction SE24 to create a class.
1. Enter a class name "ZCL_XXX" and press craete button
2. A dialog box will appear
3. Enter the description
4. Press Save button
5. Another dislog box will appear by asking you to enter the package name. Don't enter anything, Just press Local object.
6. Now it will take you to Class Builder.
7. Now you can create your own methods and write the code
8. Inorder to crate import and other parameters, place the cursor on the method and press the Parameters.
9. At last save and activate the class.
10. Now its ready for instantiating.
Hope tis helps.
Regs,
Venkat Ramanan N
2006 Jul 05 10:18 AM
Ha Rohini
Check the following Code for Simple Example of Creation of Class and Methods
&----
*& Report ZIGA_ABAPOBJECTS_ASGN01 *
*& *
&----
*& *
*& *
&----
report ziga_abapobjects_asgn01 .
parameter : p_matnr like mara-matnr.
----
CLASS lcl_material DEFINITION
----
*
----
class lcl_material definition.
public section.
data: v_matnr type mara-matnr.
methods : constructor importing matnr type mara-matnr,
write_material_desc.
private section.
data : v_maktx type makt-maktx.
methods : get_material_description.
endclass. "lcl_material DEFINITION
----
CLASS lcl_material IMPLEMENTATION
----
*
----
class lcl_material implementation.
method get_material_description.
clear v_maktx.
select single maktx into v_maktx
from makt
where matnr = v_matnr and
spras = 'E'.
endmethod. "get_material_description
method constructor.
clear v_matnr.
v_matnr = matnr.
call method get_material_description.
endmethod. "constructor
method write_material_desc.
write 😕 'Material Description :', v_maktx.
endmethod.
endclass. "lcl_material IMPLEMENTATION
data : obj type ref to lcl_material.
start-of-selection.
create object obj exporting matnr = p_matnr.
call method obj->write_material_desc.
Thanks & regards
Sreenivasulu P
2006 Jul 05 10:25 AM
Hi Rohini,
Are you new to OO ABAP?
Then, have a look at these good links-
http://www.sapgenie.com/abap/OO/
http://www.sapgenie.com/abap/OO/index.htm
http://help.sap.com/saphelp_nw04/helpdata/en/c3/225b5654f411d194a60000e8353423/content.htm
http://www.esnips.com/doc/375fff1b-5a62-444d-8ec1-55508c308b17/prefinalppt.ppt
http://www.esnips.com/doc/2c76dc57-e74a-4539-a20e-29383317e804/OO-abap.pdf
http://www.esnips.com/doc/5c65b0dd-eddf-4512-8e32-ecd26735f0f2/prefinalppt.ppt
http://www.sapgenie.com/abap/OO/
http://www.sapgenie.com/abap/OO/index.htm
http://www.sapgenie.com/abap/controls/index.htm
http://www.esnips.com/doc/2c76dc57-e74a-4539-a20e-29383317e804/OO-abap.pdf
http://www.esnips.com/doc/0ef39d4b-586a-4637-abbb-e4f69d2d9307/SAP-CONTROLS-WORKSHOP.pdf
http://www.sapgenie.com/abap/OO/index.htm
http://help.sap.com/saphelp_erp2005/helpdata/en/ce/b518b6513611d194a50000e8353423/frameset.htm
http://www.sapgenie.com/abap/OO/
Please mark useful answers.
Regards,
Tanuja.