引用:翱翔云天
继续我们的看实例学习abap面向对象编程.
在此次的例子中,我们以雇员的工资为蓝图,设计一个程序,然后再一步一步的改进我们写的程序,使其有更好的扩展性以及维护性.
该例中所使用的基类是employee类,其子类包括:boss类,不管工作多长时间,他总是有固定的周薪(好爽!).销售员类commission worker,他的收入是一小部分工资加上销售额的一定提成.计件工类piece worker,他的收入取决于生产的计件数量.
我会列出代码,基本上是一节一个类,最后是这些类的测试程序.
先给出类图如下:
第一节:employee类的实现
*&---------------------------------------------------------------------*
*& Include ZBOBO_OO_EMPLOYEE_CLASS
*&---------------------------------------------------------------------*
*定义一个基类,该类为抽象类,不能直接实例化,必须在对象中实例化
CLASS EMPLOYEE DEFINITION ABSTRACT.
PUBLIC SECTION.
METHODS:
*定义构造方法,有两个参数,分别为姓,名
CONSTRUCTOR IMPORTING A TYPE STRING
B TYPE STRING,
*定义getter方法,分别得到姓名
GETFIRSTNAME RETURNING VALUE(FI) TYPE STRING,
GETLASTNAME RETURNING VALUE(LA) TYPE STRING,
*定义两个抽象方法,因为各种employee的收入结构不同,必须在其子类中
*实例化
EARNINGS ABSTRACT
RETURNING VALUE(EARN) TYPE F,
PRINT ABSTRACT.
PRIVATE SECTION.
*定义private变量,该变量不能在子类中使用
DATA:
FIRST_NAME TYPE STRING,
LAST_NAME TYPE STRING.
ENDCLASS.
*类的实现
CLASS EMPLOYEE IMPLEMENTATION.
*初始化类
METHOD CONSTRUCTOR.
FIRST_NAME = A.
LAST_NAME = B.
ENDMETHOD.
*实现两getter方法
METHOD GETFIRSTNAME.
FI = FIRST_NAME.
ENDMETHOD.
METHOD GETLASTNAME.
LA = LAST_NAME.
ENDMETHOD.
ENDCLASS.
第二节:boss类的实现
*&---------------------------------------------------------------------*
*& Include ZBOBO_OO_BOSS_CLASS
*&---------------------------------------------------------------------*
*定义boss类,该类从employee中继承
CLASS BOSS DEFINITION INHERITING FROM EMPLOYEE.
PUBLIC SECTION.
METHODS:
*定义构造函数,初始化对象
CONSTRUCTOR IMPORTING BOSS_A TYPE STRING
BOSS_B TYPE STRING
S TYPE F,
*定义boss类的方法(setter方法)
SETWEEKLYSALARY IMPORTING WS TYPE F,
*实现基类中的抽象方法,因为boss类有自己的工资结构
*所以在此部分实现
EARNINGS REDEFINITION,
PRINT REDEFINITION.
PRIVATE SECTION.
DATA:
WEEKLYSALARY TYPE F.
ENDCLASS.
CLASS BOSS IMPLEMENTATION.
METHOD CONSTRUCTOR.
*调用父类的构造函数
CALL METHOD SUPER->CONSTRUCTOR
EXPORTING A = BOSS_A
B = BOSS_B.
*根据新的传入变量,设置boss类私有实例变量
WEEKLYSALARY = S.
ENDMETHOD.
*setter方法的实现
METHOD SETWEEKLYSALARY.
WEEKLYSALARY = WS.
ENDMETHOD.
*定义工资以及输出的实现
METHOD EARNINGS.
EARN = WEEKLYSALARY.
ENDMETHOD.
METHOD PRINT.
DATA: FIRST TYPE STRING,
LAST TYPE STRING.
FIRST = GETFIRSTNAME( ).
LAST = ME->GETLASTNAME( ).
WRITE: / 'Boss name is:',LAST,FIRST.
ENDMETHOD.
ENDCLASS.
第三节:commission worker类的实现
*&---------------------------------------------------------------------*
*& Include ZBOBO_OO_COMWORKER_CLASS
*&---------------------------------------------------------------------*
*定义commission worker类,他的收入是一小部分基本工资加上销售额的
*一定的百分比
CLASS COMMISSIONWORKER DEFINITION
INHERITING FROM EMPLOYEE.
PUBLIC SECTION.
METHODS:
*定义构造函数
CONSTRUCTOR
IMPORTING A TYPE STRING
B TYPE STRING
C TYPE F
D TYPE F
E TYPE I,
*定义setter以及getter方法
SETSALARY
IMPORTING S TYPE F,
GETSALARY
RETURNING VALUE(SA) TYPE F,
SETCOMMISSION
IMPORTING C TYPE F,
SETQUANTITY
IMPORTING Q TYPE I,
*重定义父类的抽象方法
EARNINGS REDEFINITION,
PRINT REDEFINITION.
PRIVATE SECTION.
*定义私有实例变量
DATA:
SALARY TYPE F,
COMMISSION TYPE F,
QUANTITY TYPE I.
ENDCLASS.
*实现commission worker类
CLASS COMMISSIONWORKER IMPLEMENTATION.
METHOD CONSTRUCTOR.
CALL METHOD SUPER->CONSTRUCTOR
EXPORTING A = A B = B.
SALARY = C.
COMMISSION = D.
QUANTITY = E.
ENDMETHOD.
METHOD SETSALARY.
SALARY = S.
ENDMETHOD.
METHOD GETSALARY.
SA = SALARY.
ENDMETHOD.
METHOD SETCOMMISSION.
COMMISSION = C.
ENDMETHOD.
METHOD SETQUANTITY.
QUANTITY = Q.
ENDMETHOD.
*实现工资的算法
METHOD EARNINGS.
EARN = SALARY + COMMISSION * QUANTITY.
ENDMETHOD.
*实现输出方法
METHOD PRINT.
DATA: FI TYPE STRING,
LA TYPE STRING.
WRITE: / 'Commission Worker:'.
FI = GETFIRSTNAME( ).
LA = GETLASTNAME( ).
WRITE: FI,LA.
ENDMETHOD.
ENDCLASS.
第四节:piece worker类的实现
*&---------------------------------------------------------------------*
*& Include ZBOBO_OO_PIWORKER_CLASS
*&---------------------------------------------------------------------*
*定义piece worker类,他的工资取决于他生产的计件数量
CLASS PIWORKER DEFINITION
INHERITING FROM EMPLOYEE.
PUBLIC SECTION.
METHODS:
*定义构造函数
CONSTRUCTOR
IMPORTING A TYPE STRING
B TYPE STRING
W TYPE F
Q TYPE I,
*setter方法
SETWAGE
IMPORTING W TYPE F,
SETQUANTITY
IMPORTING Q TYPE I,
*实例化抽象方法
EARNINGS REDEFINITION,
PRINT REDEFINITION.
PRIVATE SECTION.
DATA:
WAGE TYPE F,
QUAN TYPE I.
ENDCLASS.
CLASS PIWORKER IMPLEMENTATION.
METHOD CONSTRUCTOR.
CALL METHOD SUPER->CONSTRUCTOR
EXPORTING A = A B = B.
WAGE = W.
QUAN = Q.
ENDMETHOD.
METHOD SETWAGE.
WAGE = W.
ENDMETHOD.
METHOD SETQUANTITY.
QUAN = Q.
ENDMETHOD.
*计算工资
METHOD EARNINGS.
EARN = QUAN * WAGE.
ENDMETHOD.
*重定义输出方法
METHOD PRINT.
DATA: FI TYPE STRING,LA TYPE STRING.
WRITE: / 'Piece Worker:'.
FI = GETFIRSTNAME( ).
LA = GETLASTNAME( ).
WRITE: FI,LA.
ENDMETHOD.
ENDCLASS.
第五节:测试程序的实现
*&---------------------------------------------------------------------*
*& Report ZBOBO_OO_SAPLINK_004
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZBOBO_OO_SAPLINK_004.
INCLUDE ZBOBO_OO_EMPLOYEE_CLASS.
INCLUDE ZBOBO_OO_BOSS_CLASS.
INCLUDE ZBOBO_OO_COMWORKER_CLASS.
INCLUDE ZBOBO_OO_PIWORKER_CLASS.
*定义引用变量
DATA:
EM_REF TYPE REF TO EMPLOYEE,
BO_REF TYPE REF TO BOSS,
CO_REF TYPE REF TO COMMISSIONWORKER,
PI_REF TYPE REF TO PIWORKER.
DATA:
EARNS TYPE F.
START-OF-SELECTION.
*创建boss对象
CREATE OBJECT BO_REF
EXPORTING BOSS_A = 'Jhon'
BOSS_B = 'Smith'
S = '800.00'.
*narrowing cast
EM_REF = BO_REF.
*输出到屏幕
CALL METHOD EM_REF->PRINT.
EARNS = EM_REF->EARNINGS( ).
WRITE: / 'Earned: $',EARNS DECIMALS 2 EXPONENT 0.
*下面的代码基本上和上面的相同
CREATE OBJECT CO_REF
EXPORTING A = 'Sue'
B = 'Jones'
C = '800.00'
D = '3.0'
E = 150.
EM_REF = CO_REF.
CALL METHOD EM_REF->PRINT.
EARNS = EM_REF->EARNINGS( ).
WRITE: / 'Earned: $',EARNS DECIMALS 2 EXPONENT 0.
CREATE OBJECT PI_REF
EXPORTING A = 'BOB'
B = 'LEWIS'
W = '2.5'
Q = 200.
EM_REF = PI_REF.
CALL METHOD EM_REF->PRINT.
EARNS = EM_REF->EARNINGS( ).
WRITE: / 'Earned: $',EARNS DECIMALS 2 EXPONENT 0.