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

OOPS Concept

Former Member
0 Likes
657

How 2 create local Class in program

5 REPLIES 5
Read only

Former Member
0 Likes
629

HI,

see this example.

REPORT ZCLASS_EVENT.

CLASS counter DEFINITION.

PUBLIC SECTION.

METHODS increment_counter.

EVENTS critical_value EXPORTING value(excess) TYPE i.

PRIVATE SECTION.

DATA: count TYPE i,

threshold TYPE i VALUE 10.

ENDCLASS.

CLASS counter IMPLEMENTATION.

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.

ENDCLASS.

CLASS handler DEFINITION.

PUBLIC SECTION.

METHODS handle_excess

FOR EVENT critical_value OF counter

IMPORTING excess.

ENDCLASS.

CLASS handler IMPLEMENTATION.

METHOD handle_excess.

WRITE: / 'Excess is', excess.

ENDMETHOD.

ENDCLASS.

DATA: r1 TYPE REF TO counter,

h1 TYPE REF TO handler.

START-OF-SELECTION.

CREATE OBJECT: r1, h1.

SET HANDLER h1->handle_excess FOR ALL INSTANCES.

DO 20 TIMES.

CALL METHOD r1->increment_counter.

ENDDO.

***********************************************************************

REPORT ZCLASS_EVENT.

CLASS counter DEFINITION.

PUBLIC SECTION.

METHODS increment_counter.

EVENTS critical_value EXPORTING value(excess) TYPE i.

METHODS handle_excess

FOR EVENT critical_value OF counter

IMPORTING excess.

PRIVATE SECTION.

DATA: count TYPE i,

threshold TYPE i VALUE 10.

ENDCLASS.

CLASS counter IMPLEMENTATION.

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 handle_excess.

WRITE: / 'Excess is', excess.

ENDMETHOD.

ENDCLASS.

DATA: r1 TYPE REF TO counter.

START-OF-SELECTION.

CREATE OBJECT: r1.

SET HANDLER r1->handle_excess FOR ALL INSTANCES.

DO 20 TIMES.

CALL METHOD r1->increment_counter.

ENDDO.

rgds,

bharat.

Read only

Former Member
0 Likes
629

hi,

CLASS lcl_airplane DEFINITION.

PUBLIC SECTION.

METHODS: set_name importing

im_name TYPE string.

PRIVATE SECTION.

METHODS: init_name.

DATA: name TYPE string.

ENDCLASS.

CLASS lcl_airplane IMPLEMENTATION.

METHOD init_name.

name = ‘No Name‘.

ENDMETHOD.

METHOD set_name.

IF im_name IS INITIAL.

  • Calling init_name

...

ELSE. name = im_name. ENDIF.

ENDMETHOD.

ENDCLASS.

Hope this is helpful, Do reward.

Read only

Former Member
0 Likes
629

Pls refer the pgm: DEMO_ABAP_OBJECTS

Read only

Former Member
0 Likes
629

hi,

The components of the class are defined in the definition part. The components are attributes, methods,

events, constants, types and implemented interfaces. Only methods are implemented in the

implementation part.

The CLASS statement cannot be nested, that is, you cannot define a class within a class.

Attributes describe the data that can be stored in the objects in a class.

Class attributes can be of any type:

Data types: scalar (for example, data element), structured, in tables

ABAP elementary types (C, I, ...)

Object references

Interface references

In classes, you can only use the TYPE reference to refer to data types in the ABAP Dictionary.

You can only use the LIKE reference for local data objects.

The READ-ONLY addition means that a public attribute declared with DATA can be read from outside, but

can only be changed by methods within the class.

You can currently only use the READ-ONLY addition in the public visibility section (PUBLIC SECTION) of a

class declaration or in an interface definition.

You can protect attributes against access from outside by characterizing them as private attributes (defined

in the PRIVATE SECTION).

Attributes and their values that may be used directly by an external user are public attributes and are

defined in the PUBLIC SECTION.

In the above example for class lcl_airplane, the name attribute is initially defined as a public attribute and

the weight attribute is defined as a private attribute.

Public attributes belong to the class ‘external point of contact’ that is, their implementation is publicized. If

you want to hide the internal implementation from users, you must define internal and external views of

attributes.

As a general rule, you should define as few public attributes as possible.

Hope this is helpful, Do reward.

Read only

Former Member
0 Likes
629

hi

good

Global classes are the classes which we find in SE24 and call the methods of them into our program.

see the sample code

REPORT zs_class.

-


SELECTION SCREEN

-


SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.

PARAMETERS:p_var TYPE i,

p_var1 TYPE i.

SELECTION-SCREEN END OF BLOCK b1.

-


CLASS d_class DEFINITION

-


CLASS d_class DEFINITION.

PUBLIC SECTION.

METHODS:

add,

sub.

PROTECTED SECTION.

DATA : var2 TYPE i.

ENDCLASS. "d_class DEFINITION

-


CLASS d_class IMPLEMENTATION

-


CLASS d_class IMPLEMENTATION.

METHOD add.

var2 = p_var + p_var1.

WRITE:/ var2.

ENDMETHOD. "add

METHOD sub.

var2 = p_var - p_var1.

WRITE:/ var2.

ENDMETHOD. "sub

ENDCLASS. "d_class IMPLEMENTATION

-


START-OF-SELECTION

-


START-OF-SELECTION.

DATA: obj TYPE REF TO d_class.

CREATE OBJECT: obj .

CALL METHOD: obj->add,

thanks

mrutyun^