ABAP顾问
分类:
2009-02-27 17:13:46
REPORT z_barry_test_interface.
TYPE-POOLS abap.
INTERFACE if_add.
METHODS add IMPORTING summand TYPE i.
ENDINTERFACE. "if_add
INTERFACE if_subtract.
METHODS subtract IMPORTING subtrahend TYPE i.
ENDINTERFACE. "if_subtract
INTERFACE if_add_subtract.
INTERFACES: if_add,if_subtract.
ENDINTERFACE. "if_add_subtract
CLASS cl_stack DEFINITION.
PUBLIC SECTION.
INTERFACES if_add_subtract.
ALIASES: add FOR if_add~add,
subtract FOR if_subtract~subtract.
DATA amount TYPE i VALUE '100' READ-ONLY.
ENDCLASS. "cl_stack DEFINITION
CLASS cl_stack IMPLEMENTATION.
METHOD if_add~add.
amount = amount + summand.
ENDMETHOD. "if_add~add
METHOD if_subtract~subtract.
amount = amount - subtrahend.
ENDMETHOD. "if_subtract~subtract
ENDCLASS. "cl_stack IMPLEMENTATION
DATA: iref1 TYPE REF TO if_add,
iref2 TYPE REF TO if_subtract,
oref TYPE REF TO object.
DATA: ptab TYPE abap_parmbind_tab,
ptab_line LIKE LINE OF ptab,
para TYPE i.
FIELD-SYMBOLS
START-OF-SELECTION.
CREATE OBJECT iref1 TYPE cl_stack.
iref1->add( '10' ).
TRY.
iref2 ?= iref1.
iref2->subtract( '20' ).
CATCH cx_sy_move_cast_error.
MESSAGE 'Casting error' TYPE 'I'.
ENDTRY.
oref = iref2.
ASSIGN oref->('AMOUNT') TO
CALL METHOD oref->('ADD')
EXPORTING
summand = '10'.
CALL METHOD oref->('SUBTRACT')
EXPORTING
subtrahend = '10'.
WRITE