分享
 
 
 

SAP ABAP/4学习--学习使用OO在ABAP中.简单入门概念.用个例子来说明

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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( ).

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有