Chinaunix首页 | 论坛 | 博客
  • 博客访问: 710298
  • 博文数量: 145
  • 博客积分: 3446
  • 博客等级: 中校
  • 技术积分: 1567
  • 用 户 组: 普通用户
  • 注册时间: 2006-08-30 13:58
文章分类

全部博文(145)

文章存档

2021年(1)

2020年(1)

2019年(1)

2018年(6)

2017年(1)

2016年(10)

2015年(12)

2014年(10)

2013年(15)

2012年(33)

2011年(21)

2010年(9)

2009年(18)

2008年(2)

2006年(5)

我的朋友

分类:

2011-05-09 16:23:28

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.                    "Employee DEFINITION
*这里是具体的实现的
CLASS employee IMPLEMENTATION.
*实现上面的方法
  METHOD calculatepay.
    ipay = inoofunits * ipayperunit.
  ENDMETHOD.                    "CalculatePay
  METHOD writepay.
    WRITE: / 'The pay for employee ',
             nemployeeno, cemployeename,
             ' is : ', ipay.
  ENDMETHOD.                    "WritePay
ENDCLASS.                    "Employee IMPLEMENTATION
 
*定义一个子类.从父类中继承过来
CLASS factoryworker DEFINITION INHERITING FROM employee.
  PUBLIC SECTION.
*   定义个事件.
    EVENTS: lazyemployee.
*  增加一个属性
    DATA: iextranounits TYPE i.
*覆盖父的方法,现在从新定义CalculatePay.
    METHODS:
      calculatepay REDEFINITION.
ENDCLASS.                    "FactoryWorker DEFINITION
*现在实现这个个子类.
CLASS factoryworker IMPLEMENTATION.
  METHOD calculatepay.
* 从新定义方法
    ipay = ( inoofunits * ipayperunit ) +
           ( iextranounits * ipayperunit * 2 ).
*   抛出处理
    IF inoofunits < 100.
      RAISE EVENT lazyemployee.
    ENDIF.
  ENDMETHOD.                    "CalculatePay
ENDCLASS.                    "FactoryWorker IMPLEMENTATION
************************************************************************
* 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.                    "FireEmployee DEFINITION

*来实现这个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.                    "YouAreFired
ENDCLASS.                    "FireEmployee IMPLEMENTATION

*下面是使用CLASS
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->ipayperunit = 3.
  jessicajones->iextranounits = 500.      "子类中额外的一些单元
  mypay = jessicajones->calculatepay(  ).
  CALL METHOD jessicajones->writepay( mypay ).
  SKIP.
阅读(2362) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~