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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值