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.
ENDCLASS. "lcl_sub DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_sub IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_sub IMPLEMENTATION.
ENDCLASS. "lcl_sub IMPLEMENTATION
START-OF-SELECTION.
PARAMETERS:p_a TYPE i,
p_b TYPE i.
DATA:gv_add TYPE i,
ref1 TYPE REF TO lcl_sub,
ref2 TYPE REF TO lcl_super.
CREATE OBJECT ref1.
"narrowing casting up-casting
ref2 = ref1."此处的=就是narrowing casting的操作符
CALL METHOD ref2->add
EXPORTING
f_a = p_a
f_b = p_b
IMPORTING
f_c = gv_add.
WRITE:gv_add.
阅读(562) | 评论(0) | 转发(0) |