Application Development 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: 

please help

laxman_sankhla3
Participant
0 Kudos
111

hi

i need document for how to create class , method step by step .

and how to use that class and method.

thanks.

1 ACCEPTED SOLUTION

Former Member
0 Kudos
66

<b>Example 1</b>

The class circle contains two functional static methods, circumference and area, which work with the constant pi.

CLASS circle DEFINITION.

PUBLIC SECTION.

CONSTANTS pi TYPE f

VALUE '3.14159265358979324'.

CLASS-METHODS: circumference IMPORTING r TYPE f

RETURNING value(c) TYPE f,

area IMPORTING r TYPE f

RETURNING value(a) TYPE f.

ENDCLASS.

CLASS circle IMPLEMENTATION.

METHOD circumference.

c = 2 * pi * r.

ENDMETHOD.

METHOD area.

a = pi * r ** 2.

ENDMETHOD.

ENDCLASS.

DATA: circ TYPE f,

area TYPE f,

radius TYPE f.

START-OF-SELECTION.

radius = '1.00'.

circ = circle=>circumference( radius ).

area = circle=>area( radius ).

<b>Example 2</b>

When a class is first accessed, the static constructor of this class uses the system field sy-repid to set the static attribute access_program<> for the name of the program of an internal session that uses the class first.

CLASS some_class DEFINITION.

PUBLIC SECTION.

CLASS-METHODS class_constructor.

PRIVATE SECTION.

CLASS-DATA access_program TYPE sy-repid.

ENDCLASS.

CLASS some_class IMPLEMENTATION.

METHOD class_constructor.

access_program = sy-repid.

ENDMETHOD.

ENDCLASS.

<b>Example 3</b>

In the class dialog_box, a static event handler close_box id defined for the event, which is triggered when the user chooses to close a dialog box of the Control Framework (CFW).

CLASS dialog_box DEFINITION.

PUBLIC SECTION.

METHODS constructor.

...

PRIVATE SECTION.

CLASS-DATA open_boxes TYPE i.

CLASS-METHODS close_box

FOR EVENT close OF cl_gui_dialogbox_container

IMPORTING sender.

...

ENDCLASS.

CLASS dialog_box IMPLEMENTATION.

METHOD constructor.

... " create a dialogbox

open_boxes = open_boxes + 1.

ENDMETHOD.

METHOD close_box

... " close the dialogbox referred by sender

open_boxes = open_boxes - 1.

ENDMETHOD.

ENDCLASS.

Regards,

Pavan

4 REPLIES 4

gopi_narendra
Active Contributor
0 Kudos
66

Check this

Dump of the program:-

REPORT  YSUBDEL LINE-SIZE 120.

 CLASS parentclass DEFINITION .
 PUBLIC SECTION.
  DATA : commondata(30) type c value 'Accessible to all'.
  METHODS : SHOWVAL.
 PROTECTED SECTION.
  DATA : protectdata(40) type c value 'Protected data'.
 private section.
 data : privatedata(30) type c value 'Private data'.
 ENDCLASS.

 CLASS parentclass IMPLEMENTATION.
 METHOD : SHOWVAL.
  write:/5 'All data from parentclass shown:-'.
  write:/ sy-uline.
  WRITE:/5 COMMONDATA,
        /5 PROTECTDATA,
        /5 PRIVATEDATA.
 endmethod.
endclass.

CLASS childclass DEFINITION INHERITING FROM parentclass.
 PUBLIC SECTION .
 METHODS : subval.
 ENDCLASS.

 CLASS childclass IMPLEMENTATION.
 method : subval.
  skip 1.
  write:/5 'Data of parent shown from child-'.
  write:/5 sy-uline.
  WRITE:/5 COMMONDATA,
        /5 PROTECTDATA.
  Commondata = 'Public data changed in subclass'.
  Protectdata = 'Protected data changed in subclass'.
  write:/5 sy-uline.
  WRITE:/5 COMMONDATA,
        /5 PROTECTDATA.
 endmethod.
endclass.

START-OF-SELECTION.
   DATA : parent type ref to parentclass ,
          child  type ref to childclass  .
   create object : parent ,
                   child  .
   call method : parent->showval ,
                 child->subval.
      skip 2.
  parent->commondata = ‘User changing public data’.
  write:/5 parent->commondata.

Regards

Gopi

Former Member
0 Kudos
66

hi,

We can create class by using these simple steps.

class has declaration step, in this we can define call componets like

methods, attributes,evnts..

*local class creation

CLASS <class> DEFINITION.

METHOD <meth>. "for defining methods.

...

ENDMETHOD.

...

ENDCLASS.

class has implementation section, in this section we can provide implementations to all the definitions which are declared in class declaration section.

CLASS <class> IMPLEMENTATION.

...

ENDCLASS.

simple example;

CLASS C_COUNTER DEFINITION.

PUBLIC SECTION. " here declare public components

METHODS: SET_COUNTER IMPORTING VALUE(SET_VALUE) TYPE I,

INCREMENT_COUNTER,

GET_COUNTER EXPORTING VALUE(GET_VALUE) TYPE I.

PRIVATE SECTION. "here declare local components

DATA COUNT TYPE I.

ENDCLASS.

CLASS C_COUNTER IMPLEMENTATION.

METHOD SET_COUNTER. " method implemetation

COUNT = SET_VALUE.

ENDMETHOD.

METHOD INCREMENT_COUNTER.

ADD 1 TO COUNT.

ENDMETHOD.

METHOD GET_COUNTER.

GET_VALUE = COUNT.

ENDMETHOD.

ENDCLASS.

Follow these steps to declare object to class.

data: obj type ref to <name of the class>. "here refarence variable is created

CREATE OBJECT obj. "here object is created

*global class creation

we can create global class in CLASS BUILDER, for that one we work with SE24.

follow this link for class builder.

http://help.sap.com/saphelp_nw2004s/helpdata/en/ee/e440a670a111d1b44c0000e8a52bed/content.htm

follow this link for knowing types of classes and types of class components.

http://www.erpgenie.com/abap/OO/syntax.htm

http://help.sap.com/saphelp_nw04/helpdata/en/b3/f4b1406fecef0fe10000000a1550b0/content.htm

<b>Reward points if helpful</b>

Regards

Ashu

Former Member
0 Kudos
67

<b>Example 1</b>

The class circle contains two functional static methods, circumference and area, which work with the constant pi.

CLASS circle DEFINITION.

PUBLIC SECTION.

CONSTANTS pi TYPE f

VALUE '3.14159265358979324'.

CLASS-METHODS: circumference IMPORTING r TYPE f

RETURNING value(c) TYPE f,

area IMPORTING r TYPE f

RETURNING value(a) TYPE f.

ENDCLASS.

CLASS circle IMPLEMENTATION.

METHOD circumference.

c = 2 * pi * r.

ENDMETHOD.

METHOD area.

a = pi * r ** 2.

ENDMETHOD.

ENDCLASS.

DATA: circ TYPE f,

area TYPE f,

radius TYPE f.

START-OF-SELECTION.

radius = '1.00'.

circ = circle=>circumference( radius ).

area = circle=>area( radius ).

<b>Example 2</b>

When a class is first accessed, the static constructor of this class uses the system field sy-repid to set the static attribute access_program<> for the name of the program of an internal session that uses the class first.

CLASS some_class DEFINITION.

PUBLIC SECTION.

CLASS-METHODS class_constructor.

PRIVATE SECTION.

CLASS-DATA access_program TYPE sy-repid.

ENDCLASS.

CLASS some_class IMPLEMENTATION.

METHOD class_constructor.

access_program = sy-repid.

ENDMETHOD.

ENDCLASS.

<b>Example 3</b>

In the class dialog_box, a static event handler close_box id defined for the event, which is triggered when the user chooses to close a dialog box of the Control Framework (CFW).

CLASS dialog_box DEFINITION.

PUBLIC SECTION.

METHODS constructor.

...

PRIVATE SECTION.

CLASS-DATA open_boxes TYPE i.

CLASS-METHODS close_box

FOR EVENT close OF cl_gui_dialogbox_container

IMPORTING sender.

...

ENDCLASS.

CLASS dialog_box IMPLEMENTATION.

METHOD constructor.

... " create a dialogbox

open_boxes = open_boxes + 1.

ENDMETHOD.

METHOD close_box

... " close the dialogbox referred by sender

open_boxes = open_boxes - 1.

ENDMETHOD.

ENDCLASS.

Regards,

Pavan