第三代增强(BADI)基于面向对象(OO)编程思想实现,要了解第三代增强,需要对OO编程思想有一定了解。接口本身不能实例化,通过类可以来实现接口定义的方法。
Report ZINTERFACE.
* Interface Define:Ishape---形状
Interface IShape.
Methods: getArea Returning Value(area) Type F,
getCircumference Returning Value(circumference) Type F.
Endinterface.
****类定义--圆
* A Circle Class that behaves like a Shape
Class CCircle Definition.
Public Section.
Interfaces IShape.
Methods: Constructor Importing pRadius Type F.
Private Section.
Data radius Type F.
Constants PI Type F Value '3.141592365359'.
EndClass.
****类实现--圆
*Class Implementation:CCircle
Class CCircle Implementation.
******构造函数,在类实例化时系统自动调用析构含税
Method Constructor.
radius = pRadius.
EndMethod.
Method IShape~getArea.
area = PI * radius * radius.
EndMethod.
Method IShape~getCircumference.
circumference = 2 * PI * radius.
EndMethod.
EndClass.
* A Square Class----正方形
Class CSquare Definition.
Public Section.
Interfaces IShape.
Methods: Constructor Importing pSide Type F.
Private Section.
Data side Type F.
EndClass.
*Class Implementation----正方形
Class CSquare Implementation.
Method Constructor.
side = pSide.
EndMethod.
Method IShape~getArea.
Compute area = side * side.
EndMethod.
Method IShape~getCircumference.
Compute circumference = 4 * side.
EndMethod.
EndClass.
Data: CCircle_myArea Type F,
CCircle_myCircumference Type F.
Data: CSquare_myArea Type F,
CSquare_myCircumference Type F.
Data: C1 Type Ref To CCircle,
C2 Type Ref To CSquare.
START-OF-SELECTION.
Create Object C1 Exporting pRadius = '2.5'.
Create Object C2 Exporting pside = '5.0'.
Call Method C1->IShape~getArea
Receiving area = CCircle_myArea.
Call Method C1->IShape~getCircumference
Receiving circumference = CCircle_myCircumference.
Call Method C2->IShape~getArea
Receiving area = CSquare_myArea.
Call Method C2->IShape~getCircumference
Receiving circumference = CSquare_myCircumference.
Write: / 'CCircle_Area ', CCircle_myArea Decimals 4 Exponent 0.
Write: / 'CCircle_Circumference ', CCircle_myCircumference Decimals 4 Exponent 0.
Write: / 'CSquare_Area ', CSquare_myArea Decimals 4 Exponent 0.
Write: / 'CSquare_Circumference ', CSquare_myCircumference Decimals 4 Exponent 0.
运行结果:
此实例简单介绍了类和接口如何定义、实现。接口(形状)中定义了方法(面积、周长),接口不能被实例化。类(圆、正方形)实现了接口(形状)中方法(面积、周长),二者实现方式不同,这就是类的多态性。