创建程序
程序名:ZTEST_CLASS02_LHY
事务代码:SE38
输入程序名:ZTEST_CLASS02_LHY,点击“创建”按钮:
编写程序履历
*&---------------------------------------------------------------------*
*& Report ZTEST_CLASS_02_LHY
*&---------------------------------------------------------------------*
*& 类型:Report
*& 创建人:LiuHongyu
*& 创建时间:2024.1.22
*& 描述:面对对象学习测试
************************************************************************
* Changed History
*------------- ------------- --------------- ---------------
* 版本 修改人 修改时间 修改描述
*------------- ------------- --------------- ---------------
* N LiuHongyu 2024.1.22 创建
*------------- ------------- --------------- ---------------
************************************************************************
*&---------------------------------------------------------------------*
编写代码
类的定义和实现
1)定义一个car类
2)show_speed方法是用于展示speed
3)accelerrate方法有传入参数i,传出参数out_speed。
*---------------------------------------------------------------------*
* 类定义
*---------------------------------------------------------------------*
CLASS car DEFINITION.
PUBLIC SECTION.
DATA: speed TYPE i,
weight TYPE i.
METHODS:show_speed,
accelerate IMPORTING i TYPE i EXPORTING out_speed TYPE i.
ENDCLASS.
*---------------------------------------------------------------------*
* 类的实现部分
*---------------------------------------------------------------------*
CLASS car IMPLEMENTATION.
METHOD accelerate.
speed = speed + i.
out_speed = speed.
ENDMETHOD.
METHOD show_speed.
cl_demo_output=>display( speed ).
ENDMETHOD.
ENDCLASS.
完整程序
REPORT ztest_class_02_lhy.
*---------------------------------------------------------------------*
* 类定义
*---------------------------------------------------------------------*
CLASS car DEFINITION.
PUBLIC SECTION.
DATA: speed TYPE i,
weight TYPE i.
METHODS:show_speed,
accelerate IMPORTING i TYPE i EXPORTING out_speed TYPE i.
ENDCLASS.
*---------------------------------------------------------------------*
* 类的实现部分
*---------------------------------------------------------------------*
CLASS car IMPLEMENTATION.
METHOD accelerate.
speed = speed + i.
out_speed = speed.
ENDMETHOD.
METHOD show_speed.
cl_demo_output=>display( speed ).
ENDMETHOD.
ENDCLASS.
DATA obj TYPE REF TO car. "定义对象
START-OF-SELECTION.
DATA s TYPE i.
DATA outspeed TYPE i.
s = 1.
CREATE OBJECT obj. "创建对象
CALL METHOD obj->accelerate
EXPORTING i = s
IMPORTING out_speed = outspeed.
cl_demo_output=>write( outspeed ).
"WRITE:'speed:', speed.
CALL METHOD obj->show_speed( ).
创建对象obj,类型是参考car类,调用accelerate方法,传入参数s,用outspeed接受方法传出的out_speed的值。方法定义时用的是IMPORTING,调用时使用EXPORTING,定义使用EXPORTING,调用时使用IMPORTING。
RETURNING与RECEIVING
如果省略CALL METHOD,则一定要加上括号形式;如果通过CALL METHOD来调用,则括号可加可不加
RECEIVING:用来接收METHODS /CLASS-METHODS 中RETURNING选项返回的值
*&---------------------------------------------------------------------*
*& Report ZTEST_CLASS_02_LHY
*&---------------------------------------------------------------------*
*& 类型:Report
*& 创建人:LiuHongyu
*& 创建时间:2024.1.22
*& 描述:面对对象学习测试
************************************************************************
* Changed History
*------------- ------------- --------------- ---------------
* 版本 修改人 修改时间 修改描述
*------------- ------------- --------------- ---------------
* N LiuHongyu 2024.1.22 创建
*------------- ------------- --------------- ---------------
************************************************************************
*&---------------------------------------------------------------------*
REPORT ztest_class_02_lhy.
*---------------------------------------------------------------------*
* 类定义
*---------------------------------------------------------------------*
CLASS car DEFINITION.
PUBLIC SECTION.
DATA: speed TYPE i,
weight TYPE i.
METHODS:show_speed,
accelerate IMPORTING i TYPE i EXPORTING out_speed TYPE i,
decelerate IMPORTING i TYPE i
EXPORTING out_speed TYPE i
CHANGING de_speed TYPE i
RETURNING VALUE(r1) TYPE i.
ENDCLASS.
*---------------------------------------------------------------------*
* 类的实现部分
*---------------------------------------------------------------------*
CLASS car IMPLEMENTATION.
METHOD accelerate.
speed = speed + i.
out_speed = speed.
ENDMETHOD.
METHOD decelerate .
speed = speed - i.
out_speed = speed.
speed = speed - de_speed.
de_speed = de_speed + 1.
r1 = out_speed.
ENDMETHOD.
METHOD show_speed.
cl_demo_output=>display( speed ).
ENDMETHOD.
ENDCLASS.
DATA obj TYPE REF TO car. "定义对象
START-OF-SELECTION.
DATA s TYPE i.
DATA: de_i TYPE i,
de_out_speed TYPE i,
de_de_speed TYPE i,
de_r1 TYPE i.
DATA outspeed TYPE i.
de_i = 1.
de_de_speed = 7.
s = 10.
CREATE OBJECT obj. "创建对象
CALL METHOD obj->accelerate
EXPORTING i = s
IMPORTING out_speed = outspeed.
cl_demo_output=>write( '调用accelerate加速方法:' ).
cl_demo_output=>write( outspeed ). "10
"WRITE:'speed:', speed.
CALL METHOD obj->show_speed( ). "10
cl_demo_output=>write( '调用decelerate减速方法:' ).
* CALL METHOD obj->decelerate
* EXPORTING i = de_i
* IMPORTING out_speed = de_out_speed
* CHANGING de_speed = de_de_speed "CHANGING:既可以传入 也可以传出
* RECEIVING r1 = de_r1. "RECEIVING:接收
de_r1 = obj->decelerate(
EXPORTING i = de_i
IMPORTING out_speed = de_out_speed
CHANGING de_speed = de_de_speed ). "CHANGING:既可以传入 也可以传出
"RECEIVING r1 = de_r1. "RECEIVING:接收
cl_demo_output=>write( de_out_speed ). "9
cl_demo_output=>write( de_de_speed ). "8
cl_demo_output=>write( de_r1 ). "9
obj->show_speed( ).
de_r1接收decelerate方法的值:
de_r1 = obj->decelerate(
EXPORTING i = de_i
IMPORTING out_speed = de_out_speed
CHANGING de_speed = de_de_speed ). "CHANGING:既可以传入 也可以传出
方法2
使用CALL METHOD来调用decelerate方法,将dei传入方法,deout_speed接收,用r1来接收RETURNING的值:
CALL METHOD obj->decelerate
EXPORTING i = de_i
IMPORTING out_speed = de_out_speed
CHANGING de_speed = de_de_speed "CHANGING:既可以传入 也可以传出
RECEIVING r1 = de_r1. "RECEIVING:接收
* 基类
CLASS base DEFINITION.
PUBLIC SECTION.
DATA: length TYPE i, "长
width TYPE i, "宽
high TYPE i, "高
area TYPE p DECIMALS 2, "面积
volumes TYPE p DECIMALS 2. "体积
METHODS: calculate_area IMPORTING l TYPE i w TYPE i h TYPE i
EXPORTING a TYPE p.
PRIVATE SECTION.
DATA: p_area TYPE p DECIMALS 2.
ENDCLASS.
* 基类实现
CLASS base IMPLEMENTATION.
METHOD calculate_area.
length = l.
width = w.
high = h.
area = ( length * width ) / 2.
p_area = area.
a = area.
cl_demo_output=>write( area ).
ENDMETHOD.
ENDCLASS.
子类自己定义和实现了chcalculatearea方法,子类不能访问基类的私有变量,并重写了基类的calculate_area方法,重写要加上REDEFINITION关键字:
* 子类
CLASS child DEFINITION INHERITING FROM base.
PUBLIC SECTION.
CLASS-DATA: cl_child_area TYPE p DECIMALS 2.
METHODS:
calculate_area REDEFINITION, "REDEFINITION重写方法
ch_calculate_area IMPORTING l TYPE i w TYPE i h TYPE i
EXPORTING a TYPE p.
ENDCLASS.
CLASS child IMPLEMENTATION.
METHOD calculate_area."重写父类的方法
length = l.
width = w.
high = h.
area = ( length * width ) / 100.
cl_child_area = area.
"p_area = area.
a = area.
cl_demo_output=>write( area ).
ENDMETHOD.
METHOD ch_calculate_area.
length = l.
width = w.
high = h.
area = ( length * width ) / 5.
cl_child_area = area.
"p_area = area.
a = area.
cl_demo_output=>write( cl_child_area ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
*&---------------------------Variable----------------------------------*
DATA: length TYPE i,
width TYPE i,
high TYPE i,
area TYPE p DECIMALS 2.
DATA base_obj TYPE REF TO base.
DATA ch_obj TYPE REF TO child.
CREATE OBJECT base_obj.
CREATE OBJECT ch_obj.
length = 9.
width = 3.
high = 6.
*&---------------------------Logic----------------------------------*
CALL METHOD base_obj->calculate_area "调用calculate_area方法
EXPORTING l = length
w = width
h = high
IMPORTING a = area.
WRITE:/, 'area:',area.
CALL METHOD ch_obj->ch_calculate_area "调用子类的方法
EXPORTING l = length
w = width
h = high
IMPORTING a = area.
WRITE:/, 'child_area:',area.
CALL METHOD ch_obj->calculate_area "调用父类的方法
EXPORTING l = length
w = width
h = high
IMPORTING a = area.
cl_demo_output=>display( ).
WRITE:/, 'child_area:',area.
REPORT ztest_class_02_lhy.
*---------------------------------------------------------------------*
* car类定义
*---------------------------------------------------------------------*
CLASS car DEFINITION.
PUBLIC SECTION.
DATA: speed TYPE i,
weight TYPE i.
METHODS:show_speed,
accelerate IMPORTING i TYPE i EXPORTING out_speed TYPE i,
decelerate IMPORTING i TYPE i
EXPORTING out_speed TYPE i
CHANGING de_speed TYPE i
RETURNING VALUE(r1) TYPE i,
constructor. "构造函数
ENDCLASS.
*---------------------------------------------------------------------*
* car类的实现部分
*---------------------------------------------------------------------*
CLASS car IMPLEMENTATION.
METHOD accelerate.
speed = speed + i.
out_speed = speed.
ENDMETHOD.
METHOD decelerate .
speed = speed - i.
out_speed = speed.
speed = speed - de_speed.
de_speed = de_speed + 1.
r1 = out_speed.
ENDMETHOD.
METHOD show_speed.
cl_demo_output=>display( speed ).
ENDMETHOD.
METHOD constructor.
cl_demo_output=>display('constructor triggered').
ENDMETHOD.
ENDCLASS.
主程序
创建car类对象obj,调用accelerate方法,调用show_speed方法。
DATA obj TYPE REF TO car. "定义对象
START-OF-SELECTION.
DATA s TYPE i.
DATA: de_i TYPE i,
de_out_speed TYPE i,
de_de_speed TYPE i,
de_r1 TYPE i.
DATA outspeed TYPE i.
de_i = 1.
de_de_speed = 7.
s = 10.
CREATE OBJECT obj. "创建对象
CALL METHOD obj->accelerate
EXPORTING i = s
IMPORTING out_speed = outspeed.
cl_demo_output=>write( '调用accelerate加速方法:' ).
cl_demo_output=>write( outspeed ). "10
"WRITE:'speed:', speed.
CALL METHOD obj->show_speed( ). "10
* 基类
CLASS base DEFINITION.
PUBLIC SECTION.
DATA: length TYPE i, "长
width TYPE i, "宽
high TYPE i, "高
area TYPE p DECIMALS 2, "面积
volumes TYPE p DECIMALS 2. "体积
METHODS: calculate_area IMPORTING l TYPE i w TYPE i h TYPE i
EXPORTING a TYPE p.
PRIVATE SECTION.
DATA: p_area TYPE p DECIMALS 2.
ENDCLASS.
* 基类实现
CLASS base IMPLEMENTATION.
METHOD calculate_area.
length = l.
width = w.
high = h.
area = ( length * width ) / 2.
p_area = area.
a = area.
cl_demo_output=>write( area ).
ENDMETHOD.
ENDCLASS.
* 子类
CLASS child DEFINITION INHERITING FROM base.
PUBLIC SECTION.
CLASS-DATA: cl_child_area TYPE p DECIMALS 2.
METHODS:
calculate_area REDEFINITION, "REDEFINITION重写方法
ch_calculate_area IMPORTING l TYPE i w TYPE i h TYPE i
EXPORTING a TYPE p.
ENDCLASS.
CLASS child IMPLEMENTATION.
METHOD calculate_area."重写父类的方法
CALL METHOD super->calculate_area "使用super调用父类的方法
EXPORTING
l = length
w = width
h = high
IMPORTING
a = area.
cl_demo_output=>write( '重写父类的方法后,通过super调用父类的方法获取的area:' ).
cl_demo_output=>write( area ).
* length = l.
* width = w.
* high = h.
* area = ( length * width ) / 100.
* cl_child_area = area.
* "p_area = area.
* a = area.
* cl_demo_output=>write( area ).
ENDMETHOD.
METHOD ch_calculate_area.
length = l.
width = w.
high = h.
area = ( length * width ) / 5.
cl_child_area = area.
"p_area = area.
a = area.
cl_demo_output=>write( cl_child_area ).
ENDMETHOD.
ENDCLASS.
主程序
DATA obj TYPE REF TO car. "定义对象
START-OF-SELECTION.
*&---------------------------Variable----------------------------------*
DATA: length TYPE i,
width TYPE i,
high TYPE i,
area TYPE p DECIMALS 2.
DATA base_obj TYPE REF TO base.
DATA ch_obj TYPE REF TO child.
CREATE OBJECT base_obj.
CREATE OBJECT ch_obj.
length = 9.
width = 3.
high = 6.
*&---------------------------Logic----------------------------------*
CALL METHOD base_obj->calculate_area "调用calculate_area方法
EXPORTING l = length
w = width
h = high
IMPORTING a = area.
WRITE:/, 'area:',area.
CALL METHOD ch_obj->ch_calculate_area "调用子类的方法
EXPORTING l = length
w = width
h = high
IMPORTING a = area.
WRITE:/, 'child_area:',area.
CALL METHOD ch_obj->calculate_area "调用父类的方法
EXPORTING l = length
w = width
h = high
IMPORTING a = area.
cl_demo_output=>display( ).
WRITE:/, 'child_area:',area.
完整程序*&---------------------------------------------------------------------*
*& Report ZTEST_CLASS_02_LHY
*&---------------------------------------------------------------------*
*& 类型:Report
*& 创建人:LiuHongyu
*& 创建时间:2024.1.22
*& 描述:面对对象学习测试
************************************************************************
* Changed History
*------------- ------------- --------------- ---------------
* 版本 修改人 修改时间 修改描述
*------------- ------------- --------------- ---------------
* N LiuHongyu 2024.1.22 创建
*------------- ------------- --------------- ---------------
************************************************************************
*&---------------------------------------------------------------------*
REPORT ztest_class_02_lhy.
*---------------------------------------------------------------------*
* car类定义
*---------------------------------------------------------------------*
CLASS car DEFINITION.
PUBLIC SECTION.
DATA: speed TYPE i,
weight TYPE i.
METHODS:show_speed,
accelerate IMPORTING i TYPE i EXPORTING out_speed TYPE i,
decelerate IMPORTING i TYPE i
EXPORTING out_speed TYPE i
CHANGING de_speed TYPE i
RETURNING VALUE(r1) TYPE i,
constructor. "构造函数
ENDCLASS.
*---------------------------------------------------------------------*
* car类的实现部分
*---------------------------------------------------------------------*
CLASS car IMPLEMENTATION.
METHOD accelerate.
speed = speed + i.
out_speed = speed.
ENDMETHOD.
METHOD decelerate .
speed = speed - i.
out_speed = speed.
speed = speed - de_speed.
de_speed = de_speed + 1.
r1 = out_speed.
ENDMETHOD.
METHOD show_speed.
cl_demo_output=>display( speed ).
ENDMETHOD.
METHOD constructor.
cl_demo_output=>display('Constructor Triggered').
ENDMETHOD.
ENDCLASS.
* 基类
CLASS base DEFINITION.
PUBLIC SECTION.
DATA: length TYPE i, "长
width TYPE i, "宽
high TYPE i, "高
area TYPE p DECIMALS 2, "面积
volumes TYPE p DECIMALS 2. "体积
METHODS: calculate_area IMPORTING l TYPE i w TYPE i h TYPE i
EXPORTING a TYPE p.
PRIVATE SECTION.
DATA: p_area TYPE p DECIMALS 2.
ENDCLASS.
* 基类实现
CLASS base IMPLEMENTATION.
METHOD calculate_area.
length = l.
width = w.
high = h.
area = ( length * width ) / 2.
p_area = area.
a = area.
cl_demo_output=>write( area ).
ENDMETHOD.
ENDCLASS.
* 子类
CLASS child DEFINITION INHERITING FROM base.
PUBLIC SECTION.
CLASS-DATA: cl_child_area TYPE p DECIMALS 2.
METHODS:
calculate_area REDEFINITION, "REDEFINITION重写方法
ch_calculate_area IMPORTING l TYPE i w TYPE i h TYPE i
EXPORTING a TYPE p.
ENDCLASS.
CLASS child IMPLEMENTATION.
METHOD calculate_area."重写父类的方法
CALL METHOD super->calculate_area "使用super调用父类的方法
EXPORTING
l = length
w = width
h = high
IMPORTING
a = area.
cl_demo_output=>write( '重写父类的方法后,通过super调用父类的方法获取的area:' ).
cl_demo_output=>write( area ).
* length = l.
* width = w.
* high = h.
* area = ( length * width ) / 100.
* cl_child_area = area.
* "p_area = area.
* a = area.
* cl_demo_output=>write( area ).
ENDMETHOD.
METHOD ch_calculate_area.
length = l.
width = w.
high = h.
area = ( length * width ) / 5.
cl_child_area = area.
"p_area = area.
a = area.
cl_demo_output=>write( cl_child_area ).
ENDMETHOD.
ENDCLASS.
DATA obj TYPE REF TO car. "定义对象
START-OF-SELECTION.
*&---------------------------Variable----------------------------------*
DATA: length TYPE i,
width TYPE i,
high TYPE i,
area TYPE p DECIMALS 2.
DATA base_obj TYPE REF TO base.
DATA ch_obj TYPE REF TO child.
CREATE OBJECT base_obj.
CREATE OBJECT ch_obj.
length = 9.
width = 3.
high = 6.
*&---------------------------Logic----------------------------------*
CALL METHOD base_obj->calculate_area "调用calculate_area方法
EXPORTING l = length
w = width
h = high
IMPORTING a = area.
WRITE:/, 'area:',area.
CALL METHOD ch_obj->ch_calculate_area "调用子类的方法
EXPORTING l = length
w = width
h = high
IMPORTING a = area.
WRITE:/, 'child_area:',area.
CALL METHOD ch_obj->calculate_area "调用父类的方法
EXPORTING l = length
w = width
h = high
IMPORTING a = area.
cl_demo_output=>display( ).
WRITE:/, 'child_area:',area.
* DATA s TYPE i.
* DATA: de_i TYPE i,
* de_out_speed TYPE i,
* de_de_speed TYPE i,
* de_r1 TYPE i.
* DATA outspeed TYPE i.
* de_i = 1.
* de_de_speed = 7.
* s = 10.
* CREATE OBJECT obj. "创建对象
* CALL METHOD obj->accelerate
* EXPORTING i = s
* IMPORTING out_speed = outspeed.
* cl_demo_output=>write( '调用accelerate加速方法:' ).
* cl_demo_output=>write( outspeed ). "10
* "WRITE:'speed:', speed.
* CALL METHOD obj->show_speed( ). "10
* cl_demo_output=>write( '调用decelerate减速方法:' ).
** CALL METHOD obj->decelerate
** EXPORTING i = de_i
** IMPORTING out_speed = de_out_speed
** CHANGING de_speed = de_de_speed "CHANGING:既可以传入 也可以传出
** RECEIVING r1 = de_r1. "RECEIVING:接收
* de_r1 = obj->decelerate(
* EXPORTING i = de_i
* IMPORTING out_speed = de_out_speed
* CHANGING de_speed = de_de_speed ). "CHANGING:既可以传入 也可以传出
* "RECEIVING r1 = de_r1. "RECEIVING:接收
* cl_demo_output=>write( de_out_speed ). "9
* cl_demo_output=>write( de_de_speed ). "8
* cl_demo_output=>write( de_r1 ). "9
* obj->show_speed( ).
OPTIONAL:表示参数是可选的,在调用时是可选择是否传参的,你可以传递参数,也可以不传递参数。
DEFAULT:表示参数的有默认值的,是否传递参数也是可选的,你可以不传递参数,此时的默认值是定义时的默认值。如果传递参数,那么就使用你传入的值。
程序名:ZTESTCLASS03_LHY
如果使用DEFAULT,需要设置默认值:
*&---------------------------------------------------------------------*
*& Report ZTEST_CLASS_03_LHY
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ztest_class_03_lhy.
*---------------------------------------------------------------------*
* car类定义
*---------------------------------------------------------------------*
CLASS car DEFINITION.
PUBLIC SECTION.
DATA: speed TYPE i,
weight TYPE i.
METHODS: fun1 IMPORTING a TYPE i OPTIONAL,
fun2 IMPORTING b TYPE i DEFAULT 1.
endclass.
*---------------------------------------------------------------------*
* car类的实现部分
*---------------------------------------------------------------------*
CLASS car IMPLEMENTATION.
METHOD fun1.
speed = a.
cl_demo_output=>write( speed ).
ENDMETHOD.
METHOD fun2.
weight = b.
cl_demo_output=>write( weight ).
ENDMETHOD.
ENDCLASS.
不传值时的主程序
START-OF-SELECTION.
DATA a TYPE REF TO car.
CREATE OBJECT a.
CALL METHOD a->fun1.
CALL METHOD a->fun2.
cl_demo_output=>display( ).
传值时的主程序
START-OF-SELECTION.
DATA a TYPE REF TO car.
CREATE OBJECT a.
CALL METHOD a->fun1 EXPORTING a = 1000.
CALL METHOD a->fun2 EXPORTING b = 3.
cl_demo_output=>display( ).
*&---------------------------------------------------------------------*
*& Report ZTEST_CLASS_03_LHY
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ztest_class_03_lhy.
*---------------------------------------------------------------------*
* car类定义
*---------------------------------------------------------------------*
CLASS car DEFINITION.
PUBLIC SECTION.
DATA: speed TYPE i,
weight TYPE i.
METHODS: fun1 IMPORTING a TYPE i OPTIONAL,
fun2 IMPORTING b TYPE i DEFAULT 1.
endclass.
*---------------------------------------------------------------------*
* car类的实现部分
*---------------------------------------------------------------------*
CLASS car IMPLEMENTATION.
METHOD fun1.
speed = a.
cl_demo_output=>write( speed ).
ENDMETHOD.
METHOD fun2.
weight = b.
cl_demo_output=>write( weight ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA a TYPE REF TO car.
CREATE OBJECT a.
CALL METHOD a->fun1 EXPORTING a = 1000.
CALL METHOD a->fun2 EXPORTING b = 3.
* CALL METHOD a->fun1.
* CALL METHOD a->fun2.
cl_demo_output=>display( ).
接口保证这些类外部看起来具有一致性标准化的接口, 又可以在不同的类内部使用不同的实现方法。
在接口定义内部可以声明的成员与类中的成员相同, 但无需注明具体的可见性, 因为具体类中实现的所有接口组件均为公有成员. 同时, 接口定义中也只包含成员声明部分, 而具体的实现也将在具体类中进行.
*---------------------------------------------------------------------*
* r_run接口的定义
*---------------------------------------------------------------------*
INTERFACE i_run.
"INTERFACE内的都是公共的,不用刻意说明限制范围:PUBLIC / PROTECT / PRIVATE
DATA speed TYPE i.
CLASS-DATA cl_speed TYPE i.
METHODS: acc1 IMPORTING a TYPE i.
ENDINTERFACE.
acc1没有被识别到,因为没有加上i_run1~:来指明接口
加上i_run~接口:
通过类访问接口方法
使用a->i_runacc1来调用实现的接口的方法,需要使用对象->接口名方法名:
*&---------------------------------------------------------------------*
*& Report ZTEST_CLASS_03_LHY
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ztest_class_03_lhy.
*---------------------------------------------------------------------*
* r_run接口的定义
*---------------------------------------------------------------------*
INTERFACE i_run.
"INTERFACE内的都是公共的,不用刻意说明限制范围:PUBLIC / PROTECT / PRIVATE
DATA speed TYPE i.
CLASS-DATA cl_speed TYPE i.
METHODS: acc1 IMPORTING a TYPE i.
ENDINTERFACE.
*---------------------------------------------------------------------*
* car类定义
*---------------------------------------------------------------------*
CLASS car DEFINITION.
PUBLIC SECTION.
DATA: speed TYPE i,
weight TYPE i.
METHODS: fun1 IMPORTING a TYPE i OPTIONAL,
fun2 IMPORTING b TYPE i DEFAULT 1.
INTERFACES i_run.
ENDCLASS.
*---------------------------------------------------------------------*
* car类的实现部分
*---------------------------------------------------------------------*
CLASS car IMPLEMENTATION.
METHOD fun1.
speed = a.
cl_demo_output=>write( speed ).
ENDMETHOD.
METHOD fun2.
weight = b.
cl_demo_output=>write( weight ).
ENDMETHOD.
METHOD i_run~acc1.
speed = speed * a.
cl_demo_output=>write( speed ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA a TYPE REF TO car.
CREATE OBJECT a.
CALL METHOD a->fun1 EXPORTING a = 1000.
CALL METHOD a->fun2 EXPORTING b = 3.
CALL METHOD a->i_run~acc1 EXPORTING a = 3. "a->i_run~acc1,需要使用对象->接口名~方法名
* CALL METHOD a->fun1.
* CALL METHOD a->fun2.
cl_demo_output=>display( ).
第一个是调用fun1方法得到的speed,第二个是调用fun2方法得到的weight,第三个调用acc1方法得到的speed:
CALL METHOD a->i_run~acc1 EXPORTING a = 3. "a->i_run~acc1,需要使用对象->接口名~方法名
CALL METHOD intf->acc1 EXPORTING a = 8.
*&---------------------------------------------------------------------*
*& Report ZTEST_CLASS_03_LHY
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ztest_class_03_lhy.
*----------------------------------------------------------------------*
* r_run接口的定义
*---------------------------------------------------------------------*
INTERFACE i_run.
"INTERFACE内的都是公共的,不用刻意说明限制范围:PUBLIC / PROTECT / PRIVATE
DATA speed TYPE i.
CLASS-DATA cl_speed TYPE i.
METHODS: acc1 IMPORTING a TYPE i.
ENDINTERFACE.
*---------------------------------------------------------------------*
* car类定义
*---------------------------------------------------------------------*
CLASS car DEFINITION.
PUBLIC SECTION.
DATA: speed TYPE i,
weight TYPE i.
METHODS: fun1 IMPORTING a TYPE i OPTIONAL,
fun2 IMPORTING b TYPE i DEFAULT 1.
INTERFACES i_run.
ENDCLASS.
*---------------------------------------------------------------------*
* car类的实现部分
*---------------------------------------------------------------------*
CLASS car IMPLEMENTATION.
METHOD fun1.
speed = a.
cl_demo_output=>write( speed ).
ENDMETHOD.
METHOD fun2.
weight = b.
cl_demo_output=>write( weight ).
ENDMETHOD.
METHOD i_run~acc1.
speed = speed * a.
cl_demo_output=>write( speed ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA a TYPE REF TO car.
DATA intf TYPE REF TO i_run."intf接口对象
CREATE OBJECT intf TYPE car. "通过挂靠在类上来
CREATE OBJECT a.
CALL METHOD intf->acc1 EXPORTING a = 8.
CALL METHOD a->fun1 EXPORTING a = 1000.
CALL METHOD a->fun2 EXPORTING b = 3.
CALL METHOD a->i_run~acc1 EXPORTING a = 3. "a->i_run~acc1,需要使用对象->接口名~方法名
* CALL METHOD a->fun1.
* CALL METHOD a->fun2.
cl_demo_output=>display( ).
运行效果
第一个SPEED是通过intf接口变量调用acc1方法输出的:
接口类型不能赋值给非object类的类
START-OF-SELECTION.
DATA a TYPE REF TO car.
DATA intf TYPE REF TO i_run."intf接口对象
DATA myc1 TYPE REF TO object."类object是所有类的父类,且不需实例(create OBJECT会报错)
CREATE OBJECT intf TYPE car. "通过挂靠在类上
CREATE OBJECT a.
"a = intf.
intf = a.
myc1 = intf. "接口可以赋值给object类的对象
CALL METHOD intf->acc1 EXPORTING a = 8.
CALL METHOD a->fun1 EXPORTING a = 1000.
CALL METHOD a->fun2 EXPORTING b = 3.
CALL METHOD a->i_run~acc1 EXPORTING a = 3. "a->i_run~acc1,需要使用对象->接口名~方法名
* 类赋值给接口
intf = a."正常
intf = myc1.
接口类型不能赋值给非object类的类,只能赋值给object类型的:
例如:
START-OF-SELECTION.
DATA a TYPE REF TO car.
DATA intf TYPE REF TO i_run."intf接口对象
DATA intf2 TYPE REF TO i_run.
DATA myc1 TYPE REF TO object."类object是所有类的父类,且不需实例(create OBJECT会报错)
CREATE OBJECT intf TYPE car. "通过挂靠在类上来
CREATE OBJECT intf2 TYPE car.
CREATE OBJECT a.
"a = intf.
intf = a.
myc1 = intf. "接口可以赋值给object类的对象
START-OF-SELECTION.
DATA a TYPE REF TO car.
DATA intf TYPE REF TO i_run."intf接口对象
DATA myc1 TYPE REF TO object."类object是所有类的父类,且不需实例(create OBJECT会报错)
CREATE OBJECT intf TYPE car. "通过挂靠在类上
CREATE OBJECT a.
"a = intf.
intf = a.
myc1 = intf. "接口可以赋值给object类的对象
CALL METHOD intf->acc1 EXPORTING a = 8.
CALL METHOD a->fun1 EXPORTING a = 1000.
CALL METHOD a->fun2 EXPORTING b = 3.
CALL METHOD a->i_run~acc1 EXPORTING a = 3. "a->i_run~acc1,需要使用对象->接口名~方法名
* 类赋值给接口
intf = a."正常
intf = myc1.
intf = myc1.会报错。object类型的对象不能赋值给i_run接口类型,需要使用 ?= 强制转换符进行转换。
一、概念
向上转型是指将子类的实例赋值给父类类型的变量。 向下转型是指将父类的实例赋值给子类类型的变量。
二、向上转型
1、向上转型后父类的引用所指向的属性是父类的属性。 2、如果子类重写了父类的方法,那么父类引用指向的或者调用的方法是子类的方法,这个叫动态绑定(实现多态)。 3、父类引用不能调用子类自己的方法。 4、向上转型会丢失子类的新增方法,同时会保留子类重写的方法。
三、向下转型
1、编译时需要强制类型转换,father前面的必须添加(Son)。 2、运行时会出现java.lang.ClassCastException错误,可使用instanceof来避免出错此类错误。 3、向下转型可以得到子类的所有方法(包含父类的方法)。
?= 和 =
?= 向下类型转换(Down Cast )
= 向上类型转换(Up Cast )
1、向上、向下中的上下,可以理解成父类和子类。
2、向上转型(up-cast 或者 narrowing cast)指子类型引用实例化父类型对象,这样父类引用指向子类引用。这样之后父类的引用是访问不了子类的变量和方法的。用narrowing理解好点。
3、向下转型(down-cast 或者 widening cast) 父类型的引用来实例化子类型对象。这样之后子类型可以访问子类型所有方法,具体后面讲到。必须用 ?= 或者move…?to… (cast operator)
• 向上转型是把子类引用实例化父类对象。
• 向下转型是把父类引用实例化子类对象。
因为父亲有的,儿子肯定也有。所以可以直接转型。
loson = lofather.(向上)
但是儿子有的,父亲不一定有。所以需要强制转型。
lofather ?= loson.(向下)
参考文章
https://blog.csdn.net/m0_61425806/article/details/128902242
• 用于捕获某类对象状态的改变来触发事件的方法,并进行处理。
• 事件是没有继承关系的类之间可以互相调用彼此方法的特殊方法。
• 当需要关联两个互不关联的类时,需要通过定义事件来实现
事件可以在类或接口的声名部分进行声明.
定义:可以在类或接口中进行声明:
EVENTS|CLASS-EVENTS evt
EXPORTING … VALUE(p1)TYPE type|LIKE f [OPTIONAL|DEFAULT g] …
例:
EVENTS mod_10 EXPORTING VALUE(p_data) TYPE i.
实例事件中包含一个隐含参数SENDER,该参数的类型为触发时间的类或接口对象引用。
非静态事件只能在非静态方法中触发,而不能在静态方法中触发;而静态事件即可在静态也可在非静态方法中进行触发,或者反过来说:实例方法既可触发静态事件,也可触发非静态事件,但静态方法就只能触发静态事件。
RAISE EVENT mod_10 EXPORTING p_data = t_data."触发事件
事件需要通过方法捕获事件并进行处理, 必须首先为该事件定义一个事件处理方法, 然后在运行时为该事件进行注册。
METHODS write_data FOR EVENT mod_10 OF cl_data IMPORTING p_data."接收事件
要使事件处理方法能够对事件进行响应,必须在运行时为相关事件注册该方法:
SET HANDLER cl_handl->write_data FOR cl_event."注册事件,才可以进行两个类之间的变量传值
对于实例事件, 在进行处理之前必须在注册语句中使用附加项 FOR 指定希望注册到的对象.
可以指定一个单独的对象作为事件触发者。
SET HANDLER ... hi ... FOR ref.
或者使用 FOR ALL INSTANCES 附加项为处理器注册所有可以触发该事件的实例;
SET HANDLER ... hi ... FOR ALL INSTANCES.
该注册过程可以作用于目前尚未被创建的实例。
SET HANDLER ... hi ...
程序名:ZTESTCLASS03_LHY
*---------------------------------------------------------------------*
* cl_data类定义
*---------------------------------------------------------------------*
CLASS cl_data DEFINITION.
PUBLIC SECTION. "公有
METHODS add_data.
EVENTS mod_10 EXPORTING VALUE(p_data) TYPE i."定义事件
PRIVATE SECTION. "私有
DATA t_data TYPE i.
ENDCLASS.
*---------------------------------------------------------------------*
* cl_data类实现
*---------------------------------------------------------------------*
CLASS cl_data IMPLEMENTATION.
METHOD add_data.
DATA gv_data TYPE i.
t_data = t_data + 1.
gv_data = t_data MOD 10."取余
IF gv_data = 0."如果余数等于0
RAISE EVENT mod_10 EXPORTING p_data = t_data."触发事件
ENDIF.
ENDMETHOD.
ENDCLASS.
*---------------------------------------------------------------------*
* lcl_handler类定义
*---------------------------------------------------------------------*
CLASS lcl_handler DEFINITION.
PUBLIC SECTION.
METHODS write_data FOR EVENT mod_10 OF cl_data IMPORTING p_data."接收事件
ENDCLASS.
*---------------------------------------------------------------------*
* lcl_handler类实现
*---------------------------------------------------------------------*
CLASS lcl_handler IMPLEMENTATION.
METHOD write_data.
WRITE:/ p_data.
ENDMETHOD.
ENDCLASS.
DATA cl_event TYPE REF TO cl_data."创建对象
DATA cl_handl TYPE REF TO lcl_handler. "创建对象
START-OF-SELECTION.
CREATE OBJECT cl_handl."实例化
CREATE OBJECT cl_event. "实例化
SET HANDLER cl_handl->write_data FOR cl_event."注册事件,才可以进行两个类之间的变量传值
DO 50 TIMES.
CALL METHOD cl_event->add_data.
ENDDO.