REPORT zthomas_oo_casting.
"class definition
*----------------------------------------------------------------------*
* CLASS lcl_super DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_super DEFINITION .
PUBLIC SECTION.
METHODS:add
IMPORTING f_a TYPE i
f_b TYPE i
EXPORTING f_c TYPE i.
ENDCLASS. "lcl_super DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_super IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_super IMPLEMENTATION.
METHOD add.
f_c = f_a + f_b.
ENDMETHOD. "add
ENDCLASS. "lcl_super IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS lcl_sub DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_sub DEFINITION INHERITING FROM lcl_super.
PUBLIC SECTION.
METHODs add REDEFINITION.
METHODs sub
IMPORTING f_a TYPE i
f_b TYPE i
EXPORTING f_c TYPE i..
ENDCLASS. "lcl_sub DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_sub IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_sub IMPLEMENTATION.
METHOD add.
f_c = f_a + f_b + 1.
ENDMETHOD.
METHOD sub.
f_c = f_a - f_b .
ENDMETHOD.
ENDCLASS. "lcl_sub IMPLEMENTATION
START-OF-SELECTION.
PARAMETERS:p_a TYPE i,
p_b TYPE i.
DATA:gv_add TYPE i,
gv_sub TYPE i,
ref1 TYPE REF TO lcl_sub,
ref3 TYPE REF TO lcl_sub,
ref2 TYPE REF TO lcl_super.
CREATE OBJECT ref3.
ref2 = ref3.
ref1 ?= ref2.
call METHOD ref1->add
EXPORTING f_a = p_a
f_b = p_b
IMPORTING f_c = gv_add.
CALL METHOD ref1->sub
EXPORTING f_a = p_a
f_b = p_b
IM
PORTING f_c = gv_sub.
WRITE:gv_add,gv_sub.
这个widening casting不能类比narrowing casting,具体分析见下图中sap bc404中解释
阅读(682) | 评论(0) | 转发(0) |