Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Simple Class Example

Former Member
0 Kudos
237

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.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Kudos
189

You can build a Global class in transaction SE24.

5 REPLIES 5
Read only

Former Member
0 Kudos
190

You can build a Global class in transaction SE24.

Read only

Former Member
0 Kudos
189

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.

Read only

Former Member
0 Kudos
189

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

Read only

Former Member
0 Kudos
189

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