ABAP类的继承、多态、重载

xiaoxiao2021-02-28  33

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.

转载请注明原文地址: https://www.6miu.com/read-2624577.html

最新回复(0)