SAP ABAP/4ѧϰ--ѧϰʹÓÃOOÔÚABAPÖÐ.¼òµ¥ÈëßÅÄî.ÓøöÀý×ÓÀ´ËµÃ÷

Àà±ð£º±à³ÌÓïÑÔ µã»÷£º0 ÆÀÂÛ£º0 ÍƼö£º

ABAP Object Oriented Programming

 

 

 

 

REPORT ZHEF_ABAP_OBJECTS_EXAMPLE2.

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

* Local classes Example 2

* Using enheritance

* Raising and handling events

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

 

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

* Super class Empolyee

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

 

*¶¨ÒåÒ»¸ö¸¸Àà,Ö»ÊǶ¨ÒåÀïÃæµÄһЩÊý¾ÝÀàÐÍ,

* ¾ßÌåµÄ·½·¨ºÍÄÚÈÝÔÚÏÂÃæʵÏÖ.

CLASS Employee DEFINITION.

*¶¨Ò幫¹²²¿·ÖµÄÊý¾ÝºÍ·½·¨

PUBLIC SECTION.

  DATA:

      nEmployeeNo(4)       type n,   “¹ÍÔ±Ô±¹¤ºÅ

      cEmployeeName(30)    type c,   “¹ÍÔ±ÐÕÃû

      iNoOfUnits           type i,   “Éú²úµÄµÄµ¥ÔªµÄÊýÁ¿

      iPayPerUnit          type i.   “ÿ¸öµ¥ÔªµÄ¼Û¸ñ

 

*¶¨ÒåÁ½¸ö·½·¨,Ò»¸öÊÇÊä³ö,Ò»¸öÊÇÊäÈë

  METHODS:

      CalculatePay RETURNING VALUE(iPay) TYPE i,

      WritePay     IMPORTING VALUE(iPay) TYPE i.

ENDCLASS.

 

 

*ÕâÀïÊǾßÌåµÄʵÏÖµÄ

CLASS Employee IMPLEMENTATION.

*ʵÏÖÉÏÃæµÄ·½·¨

  METHOD CalculatePay.

 

    iPay = iNoOfUnits * iPayPerUnit.

  ENDMETHOD.

 

  METHOD WritePay.

 

    WRITE: / 'The pay for employee ',

             nEmployeeNo, cEmployeeName,

             ' is : ', iPay.

  ENDMETHOD.

 

ENDCLASS.

 

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

* Subclass Factory Worker

*

* The class FactoryWorker is a subclass of the superclass Employee.

* Note that the attribute iExtraNoUnits have been added and the

* method CalculatePay has been redefined.

 

*

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

 

 

 

 

 

 

 

*¶¨ÒåÒ»¸ö×ÓÀà.´Ó¸¸ÀàÖм̳йýÀ´

CLASS FactoryWorker DEFINITION INHERITING FROM Employee.

  PUBLIC SECTION.

*   ¶¨Òå¸öʼþ.  

EVENTS: LazyEmployee.  

 

*  Ôö¼ÓÒ»¸öÊôÐÔ 

    DATA: iExtraNoUnits type i.

 

*¸²¸Ç¸¸µÄ·½·¨,ÏÖÔÚ´Óж¨ÒåCalculatePay.

    METHODS:

*     Redefinition af the CalculatePay method from the Superclass

      CalculatePay REDEFINITION.

 

ENDCLASS.

 

*ÏÖÔÚʵÏÖÕâ¸ö¸ö×ÓÀà.

CLASS FactoryWorker IMPLEMENTATION.

  METHOD CalculatePay.

* ´Óж¨Òå·½·¨

 

    iPay = ( iNoOfUnits * iPayPerUnit ) +

           ( iExtraNoUnits * iPayPerUnit * 2 ).

          

*   Å׳ö´¦Àí

    IF iNoOfUnits < 100.

      raise event LazyEmployee.     

    ENDIF.

  ENDMETHOD.

 

ENDCLASS.

 

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

* This class only to show how you can handle an event that have been

* raised in another class

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

* Õâ¸öÀàÖ»ÄÜ´¦ÀíÒ»¸öÔÚÆäËûÀàÖÐÅ׳öµÄʼþ.

* ±»¿ª³ýµÄÔ±¹¤µÄÀàµÄ¶¨Òå

CLASS FireEmployee DEFINITION.

 

  PUBLIC SECTION.

*   Method for handling the event LazyEmployee in the FactoryWorker

*   subclass.

*   ¶¨ÒåYOUAREFIRED·½·¨ÎªFACTORYWORKERÀàÖеÄLAZYEMPLOYEEʼþ.  

    METHODS YouAreFired

            FOR EVENT LazyEmployee

            OF FactoryWorker.

ENDCLASS.

 

 

 

 

 

 

 

 

*À´ÊµÏÖÕâ¸öCLASS°É

* È»ºóʵÏÖ¾ßÌåµÄ·½·¨,¾ÍÊÇÊä³öÒ»¾ä»°,¾ßÌå´¦Àí.

CLASS FireEmployee IMPLEMENTATION.

  METHOD YouAreFired.   

    WRITE:/ 'You lazy worker, you have produced less than 100 units.'

           color col_heading,

           'YOU ARE FIRED !!!' color col_negative intensified.

 

  ENDMETHOD.

ENDCLASS.

 

 

*ÏÂÃæÊÇʹÓÃCLASS

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

* U S I N G   T H E   C L A S S E S

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

 

DATA: MyPay TYPE i.

 

START-OF-SELECTION.

*-----------------------------------------------------------------------

* Using the superclass Employee

*-----------------------------------------------------------------------

 

* Declare new Employee from the EmployeeClass

 

  DATA: SamSmith TYPE REF TO Employee.

 

  CREATE OBJECT: SamSmith.

  SamSmith->nEmployeeNo = 433.

  SamSmith->cEmployeeName = 'Sam Smith'.

  SamSmith->iNoOfUnits = 150.

  SamSmith->iPayPerUnit = 3.

  MyPay = SamSmith->CalculatePay(  ).

  CALL METHOD SamSmith->WritePay( MyPay ).

  SKIP.

 

*-----------------------------------------------------------------------

* Using the subclass FactoryWorker

*-----------------------------------------------------------------------

 

* Declare new FactoryWorker as subclass of the EmployeeClass

  DATA: JessicaJones TYPE REF TO FactoryWorker.

 

  CREATE OBJECT: JessicaJones.

 

  JessicaJones->nEmployeeNo = 1500.

  JessicaJones->cEmployeeName = 'Jessica Jones'.

  JessicaJones->iNoOfUnits = 300.

  JessicaJones->iExtraNoUnits = 500.      “×ÓÀàÖжîÍâµÄһЩµ¥Ôª

  JessicaJones->iPayPerUnit = 3.

  MyPay = JessicaJones->CalculatePay(  ).

  CALL METHOD JessicaJones->WritePay( MyPay ).

  SKIP.

 

 

 

* ÒÔÏÂÊÇ´¦ÀíÒ»¸öʼþËùдµÄÀý×Ó,ÉÏÃæµÄÁ½¸ö,Ò»¸öÓõ½¸¸Àà,Ò»¸öÓõ½ÁË×ÓÀà.

* ÏÖÔÚÊÇÓõ½Ê¼þ´¦ÀíµÄÀý×Ó.

 

 

*-----------------------------------------------------------------------

 

* Using the subclass FactoryWorker with event

*-----------------------------------------------------------------------

*¶¨Òå¸öÔ±¹¤ÊµÀý

*ÔÙ¶¨Òå¸ö´¦ÀíʼþµÄ¶ÔÏó

 

  DATA: LazyJohn TYPE REF TO FactoryWorker,

        FireHim TYPE REF TO FireEmployee.

 

  CREATE OBJECT:  LazyJohn,

                  FireHim.

 

 

 

* The SET HANDLER statement, registers a runtime trigger.

* It links a list of handler methods to corresponding event triggers

* Each SET HANDLER statement creates an entry in an event handler table

 

* ΪLAZYJOHNÉèÖÃÒ»¸ö´¦ÀíHANDLER ,¾ßÌåµÄ·½·¨ÊÇ

  SET HANDLER FireHim->YouAreFired FOR LazyJohn.

 

  LazyJohn->nEmployeeNo = 1500.

  LazyJohn->cEmployeeName = 'Lazy John'.

  LazyJohn->iNoOfUnits = 90.

  LazyJohn->iExtraNoUnits = 500.

  LazyJohn->iPayPerUnit = 3.

  WRITE: / 'Lazy John has produced less than 100 units'.

  MyPay = LazyJohn->CalculatePay(  ).

 

 

±¾ÎĵØÖ·£ºhttp://com.8s8s.com/it/it23740.htm