SAP ABAP4 Learning - Learning to use oo in ABAP. Simple entry concept. Use an example to explain

xiaoxiao2021-03-06  39

ABAP Object Oriented Programming

Report zhef_abap_Objects_example2.

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

* Local Classes EXAMPLE 2

* Using en Heritance

* Raising and Handling Events

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

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

* Super Class Empolyee

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

* Define a parent class, just define some of the data types in it,

* The specific method and content are implemented below.

Class Employee Definition.

* Define the data and methods of the public part

Public section.

Data:

Nemployeeno (4) Type N, "Employee Number

CEMPLOYEENAME (30) TYPE C, "Employee Name

Inoofunits Type I, "The number of units produced

iPayPerUnit Type I. "The price of each unit

* Define two methods, one is output, one is input

Methods:

CalculatePay Returning Value (iPay) Type i,

WritePay Importing Value (iPay) Type I.

Endclass.

* Here is a specific implementation

Class Employee Implementation.

* Implement the above method

Method CalculatePay.

iPay = inooffInits * 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.

*

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

* Define a subclass. Inherited from the parent class

Class FactoryWorker Definition Inheriting from Employee.

Public section.

* Define an event.

Events: lazyemployee.

* Add an attribute

Data: IEXTRANOUNITS TYPE I.

* Cover the father's method and now define CalculatePay.

Methods:

* Redefinition Af The CalculatePay Method from the superclasscalculatepay redefinition.

Endclass.

* This subclass is now realized.

Class FactoryWorker IMPLEMENTATION.

Method CalculatePay.

* From the new definition method

iPay = (inoofunits * iPayperUnit)

Iextranounits * iPayPerunit * 2).

* Throwing process

IF inoofunits <100.

Raise Event LazyemPloyee.

ENDIF.

Endmethod.

Endclass.

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

* This class only to show hope you can handle an Event this has been

* raised in another class

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

* This class can only process an event thrown in other classes.

* Definition of class of employees being opened

Class FireemPloyee Definition.

Public section.

* Method for Handling The Event Lazyemployee in The FactoryWorker

* Subclass.

* Define YouareFired methods to the LazyemPloyee event in the FactoryWorker class.

Methods youarefired

For Event LazyemPloyee

OF FACTORYWORKER.

Endclass.

* To achieve this class

* Then implement the specific method, it is to output a sentence, and process it.

Class FireemPloyee Implementation.

Method youarefired.

Write: / 'you lazy worker, you have products.'

Color col_Heading,

'You are fas Fired !!!' color color color color color color.

Endmethod.

Endclass.

* Below is the use of 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-> iptranounits = 500. "Some units in subclats

Jessicajones-> iPayperUnit = 3.

MYPAY = JESSICAJONES-> CALCCULATEPAY ().

Call method Jessicajones-> WritePay (MyPay).

SKIP.

* The following is to process the example written by an event, the above, one for the parent class, one for the child.

* Now is an example of an event processing.

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

* Using The Subclass FactoryWorker with Event

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

* Define an employee instance

* Define the object of the handling 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

* Set a handleler for lazyjohn, the specific method is

Set Handler Firehim-> YouAarefired 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 ().

转载请注明原文地址:https://www.9cbs.com/read-71820.html

New Post(0)