Friday, August 19, 2011

Using Methods in Class


Methods describe the behavior of objects within a class. With the help of methods, the system provides operations, services and functions. Via methods, a user can manipulate the objects in a class or also the class itself. As for attributes, there are instance-dependent as well as class-dependent (static) methods. ABAP Objects differentiate between instance-dependent and class-dependent methods via the ABAP keywords METHODS or CLASS-METHODS used in the definition part.
In order to carry out instance-dependent (or instance-dependent) methods, the calling program needs a specific instance of the class. That is, the calling program must have a defined reference variable that points to a specific instance. Class methods are not instance-dependent. They can be called at any time by a user. To see how the syntax calls the various method types, see the following example.
REPORT  Z_002_METHODS.
TYPES: TY_NAME(50) TYPE C,
       BOOLEAN(1) TYPE C.

CONSTANTS: CO_TRUE TYPE C VALUE 'X',
           CO_FALSE TYPE C VALUE ' '.
*----------------------------------------------------------------
*       CLASS CL_NAME DEFINITION
*----------------------------------------------------------------
CLASS CL_NAME DEFINITION.
  PUBLIC SECTION.
    CLASS-DATA I_COUNT TYPE I.   " Class Dependent Attribute
                        " Will have same value in all objects
    DATA NAME TYPE TY_NAME.      " Instance Dependent Attribute
               " Will have different values in different objects
    CLASS-METHODS IS_CLASS_INITIATED
                    EXPORTING VALUE(RE_BOOL) TYPE BOOLEAN.
    METHODS: SET_NAME IMPORTING VALUE(IM_NAME) TYPE TY_NAME,
             GET_NAME EXPORTING VALUE(EX_NAME) TYPE TY_NAME.
ENDCLASS.                    "CL_NAME DEFINITION
*----------------------------------------------------------------
*       CLASS CL_NAME IMPLEMENTATION
*----------------------------------------------------------------
CLASS CL_NAME IMPLEMENTATION.
  METHOD IS_CLASS_INITIATED.
    IF I_COUNT > 0.
      RE_BOOL = CO_TRUE.
    ELSE.
      RE_BOOL = CO_FALSE.
    ENDIF.
  ENDMETHOD. "IS_CLASS_INITIATED
  METHOD SET_NAME.
    NAME = IM_NAME.
  ENDMETHOD. "SET_NAME
  METHOD GET_NAME.
    EX_NAME = NAME.
  ENDMETHOD. "GET_NAME
ENDCLASS.                    "CL_NAME IMPLEMENTATION
START-OF-SELECTION.
  DATA BOOL TYPE BOOLEAN.
 
" CHECK IF ANY INSTANCE OF THIS CLASS HAS BEEN CREATED BEFORE
  CALL METHOD CL_NAME=>IS_CLASS_INITIATED
    IMPORTING
      RE_BOOL = BOOL.
  IF BOOL = CO_FALSE.
    " CREATE INSTANCE OF THIS CLASS, IF NO INSTANCE EXIST
    DATA: INSTANCE TYPE REF TO CL_NAME,
          NAME TYPE TY_NAME.
    CREATE OBJECT INSTANCE.
    " SET VALUE FOR NAME ATTRIBUTE
    CALL METHOD INSTANCE->SET_NAME
      EXPORTING
        IM_NAME = 'VINOTH'.
    " GET VALUE OF NAME ATTRIBUTE
    CALL METHOD INSTANCE->GET_NAME
      IMPORTING
        EX_NAME = NAME.
    WRITE: / 'NAME : ' , NAME.
    " CLEAR MEMORY OCCUPIED BY OBJECTS
    CLEAR: INSTANCE.
  ENDIF.
Program Output : 002
NAME :  VINOTH

No comments:

Post a Comment