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( ).
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/20163.html