ABAP类的继承、多态、重载
REPORT?demo_list_hide?NO?STANDARD?PAGE?HEADING.
*-----------------------------
class?superclass?definition.?定義基類
??public?section.
????data?para(30)?type?c?value?'this?is?super?method!'.
????methods?write_first.
????methods?write.
endclass.
class?subclass?definition?inheriting?from?superclass.?定義派生類
??public?section.
????methods?write_first?redefinition.?定義重載方法
????methods?write_second.
endclass.
class?superclass?implementation.
??method?write_first.
????write:/?para.
????skip.
??endmethod.
??method?write.
????write:/?'super?method?not?inheriting!'.
??endmethod.
endclass.
class?subclass?implementation.
??method?write_first.
????call?method?super->write_first.?通過super調用基類的方法,反之me
??endmethod.
??method?write_second.
????para?=?'the?redifinition?method!'.
????call?method?me->write_first.?通過me調用自身的方法
??endmethod.
endclass.
*------------------------------------------------------
*-基類有多個派生類的時候,所有操作都在派生類中實現,并不需要通過基類定義對象實例,可以將基類定義為一個模板,聲明為抽象類。
*-抽象類包含至少一個抽象方法,不能通過create?object聲明對象實例。抽象方法只包含方法的定義,不包含具體的實現,實現在子類中。
class?superclass2?definition?abstract.??定義一個抽象類
??public?section.
????data?abs(40)?value?'this?is?a?super?abstract!'.
????methods?write_first?abstract.定義抽象方法
endclass.
class?subclass2?definition?inheriting?from?superclass2.
??public?section.
????methods?write_first?redefinition.
????methods?write_second.
endclass.
class?subclass2?implementation.
??method?write_first.
????write:/?'abstract:'.
????write:/?abs.
????skip.
??endmethod.
??method?write_second.
????write:/?'this?is?the?second?method.'.
??endmethod.
endclass.
data?new2?type?ref?to?subclass2.
data?new?type?ref?to?subclass.
start-of-selection.
??create?object?new.
??call?method:?new->write_first,?new->write_second,?new->write.派生類的實例化對象可以調用自身的所有方法,而且可以調用基類的方法,但是方法重載之后,不能通過實例化對象直接調用該方法,需用supper來實現。
create?object?new2.
call?method:?new2->write_first,?new2->write_second.
最終類與最終方法:
最終類和最終方法都是不可以繼承的,為了防止多級別的派生導致的語義語法沖突。
class?finalclass?definition?final.
…………
methods?final_write?final.
endclass.
?
總結
以上是生活随笔為你收集整理的ABAP类的继承、多态、重载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: smartforms打印不了可能问题
- 下一篇: SAP Basis 应该做什么