REPORT ZHEF_ABAP_OBJECTS_EXAMPLE1.
************************************************************************
* Local classes Example 1
*
* Define and implement DOG a class
* Adding attributes, methods and events, raising an event.
* Create a new dog from the dog class
* Using the dog class
*
*
* Henrik Frank 19-01-2000
************************************************************************
CLASS DOG DEFINITION.
*********************************************************************
* D E F I N I N G T H E C L A S S
*********************************************************************
PUBLIC SECTION.
* Public section: Can be accessed by all clients of the class
TYPES: TTYPE_OF_DOG(10) TYPE C.
* Static attribute, applies to all instances
CLASS-DATA: TYPE_OF_ANIMAL(10) TYPE C VALUE 'Dog'.
* Instance attribut, can be different for each instance
DATA: TYPE_OF_DOG TYPE TTYPE_OF_DOG VALUE 'Bulldog',
DATE_OF_BIRTH TYPE D.
* Methods with examples of Importing and Exporting parameters.
* You could also have used a Changing parameter
METHODS:
BARK,
EAT IMPORTING TYPE_OF_FOOD TYPE I,
GET_AGE_OF_DOG RETURNING VALUE(AGE_OF_DOG) TYPE I.
* Events
EVENTS:
NOT_A_BABY.
PROTECTED SECTION.
* Protected section: Can be accessed in the methods of the class and
* its subclasses
CONSTANTS: TYPE_OF_FOOD_FISH TYPE I VALUE 1,
TYPE_OF_FOOD_BONE TYPE I VALUE 2.
PRIVATE SECTION.
* Can only be accessed in the methods off the class
ENDCLASS.
CLASS DOG IMPLEMENTATION.
*********************************************************************
* I M P L E M E N T A T I O N O F T H E C L A S S
*********************************************************************
METHOD BARK.
* A non parameterized method, that doesn't return a value
WRITE: / 'Baauuu Waauu' COLOR COL_NEGATIVE INVERSE.
ENDMETHOD.
METHOD EAT.
* A parameterized method, that doesn't return a value
IF TYPE_OF_FOOD = TYPE_OF_FOOD_FISH.
WRITE: / 'Bwadrrrrr' COLOR COL_NEGATIVE INVERSE.
ELSE.
WRITE: / 'Yahooo' COLOR COL_NEGATIVE INVERSE.
ENDIF.
ENDMETHOD.
METHOD GET_AGE_OF_DOG.
* A non parameterized method, that returns a value and
* uses an attribut
AGE_OF_DOG = SY-DATUM - DATE_OF_BIRTH.
* Raise an event
IF AGE_OF_DOG > 100.
RAISE EVENT NOT_A_BABY.
ENDIF.
ENDMETHOD.
ENDCLASS.
*********************************************************************
* U S I N G T H E C L A S S
*********************************************************************
* Declare new dog of the dog class
DATA: MY_DOG TYPE REF TO DOG,
AGE TYPE I.
START-OF-SELECTION.
* Create the dog
CREATE OBJECT: MY_DOG.
*-------------------------------------------------------------------
* Do something with the dog
*-------------------------------------------------------------------
* Setting an Instance attribut
MY_DOG->DATE_OF_BIRTH = '19990701'.
* Getting a Static attribute
WRITE:/ 'What kind of animal are you ?'.
WRITE: / MY_DOG->TYPE_OF_ANIMAL COLOR COL_NEGATIVE INVERSE..
SKIP.
* Getting an Instance attribut
WRITE:/ 'What kind of dog are you ?'.
WRITE: / MY_DOG->TYPE_OF_DOG COLOR COL_NEGATIVE INVERSE.
SKIP.
* Invoking af method of the dog class that returns a value
WRITE: / 'How old are you'.
AGE = MY_DOG->GET_AGE_OF_DOG( ).
WRITE: / AGE COLOR COL_NEGATIVE INVERSE,
'days' COLOR COL_NEGATIVE INVERSE.
SKIP.
* Invoking a non parameterized method, that doesn't
* return a value
WRITE: / 'BARK !'.
CALL METHOD MY_DOG->BARK.
SKIP.
* 2 examples of invoking a parameterized method
WRITE: / 'Eat a fish !'.
CALL METHOD MY_DOG->EAT( 1 ).
SKIP.
WRITE: / 'Eat a bone !'.
CALL METHOD MY_DOG->EAT( 2 ).