ABAP接口用法详解编程语言

1、定义接口
INTERFACE intf [PUBLIC].
  [components]
ENDINTERFACE.

2、注意点:

2.1、接口中所定义的所有东西默认都是公共的,所以不用也不能写PUBLIC SECTION。

2.2、只有在接口中定义的常量(CONSTANTS)才可以使用 VALUE 选项来初始化,而DATA、CLASS-DATA定义的变量是不能初始化

2.3、接口定义中也只能包含成员声明部分,而具体的实现将在具体类中进行

2.4、如果要实现某个接口,则在类的公共声明PUBLIC SECTION部分进行实现,其余部分不行

3、定义一个接口 MediaPlayer,表示家庭影院的一个设备。MediaPlayer 中包含 play(),stop(), open()三个方法,分别表示播放、停止和开仓功能。
MediaPlayer 有三个实现类,分别为: DVDPlayer,表示 DVD 播放器; CDPlayer,表示 CD 播放器;TapePlayer,表示录音机(播放磁带)。

INTERFACE mediaplayer. 
CONSTANTS pi TYPE f VALUE '3.1415926'. 
CLASS-DATA dd type f . 
METHODS: play,stop,open. 
ENDINTERFACE. 
CLASS dvdplayer DEFINITION. 
PUBLIC SECTION. 
INTERFACES mediaplayer. 
ENDCLASS. 
CLASS dvdplayer IMPLEMENTATION. 
METHOD mediaplayer~play. 
WRITE / 'dvd is play'. 
ENDMETHOD. 
METHOD mediaplayer~stop. 
WRITE / 'dvd is stop'. 
ENDMETHOD. 
METHOD mediaplayer~open. 
WRITE / 'dvd is open'. 
ENDMETHOD. 
ENDCLASS. 
CLASS cdplayer DEFINITION. 
PUBLIC SECTION. 
INTERFACES mediaplayer. 
ENDCLASS. 
CLASS cdplayer IMPLEMENTATION. 
METHOD mediaplayer~play. 
WRITE / 'cd is play'. 
ENDMETHOD. 
METHOD mediaplayer~stop. 
WRITE / 'cd is stop'. 
ENDMETHOD. 
METHOD mediaplayer~open. 
WRITE / 'cd is open'. 
ENDMETHOD. 
ENDCLASS. 
CLASS tapeplayer DEFINITION. 
PUBLIC SECTION. 
INTERFACES mediaplayer. 
ENDCLASS. 
CLASS tapeplayer IMPLEMENTATION. 
METHOD mediaplayer~play. 
WRITE / 'tape is play'. 
ENDMETHOD. 
METHOD mediaplayer~stop. 
WRITE / 'tape is stop'. 
ENDMETHOD. 
METHOD mediaplayer~open. 
WRITE / 'tape is open'. 
ENDMETHOD. 
ENDCLASS. 
*声明对象 
DATA: dvd TYPE REF TO dvdplayer, 
cd TYPE REF TO cdplayer, 
tape TYPE REF TO tapeplayer. 
START-OF-SELECTION. 
CREATE OBJECT dvd. 
CREATE OBJECT cd. 
CREATE OBJECT tape. 
CALL METHOD dvd->mediaplayer~play. 
CALL METHOD cd->mediaplayer~stop. 
CALL METHOD tape->mediaplayer~open. 
write / dvd->mediaplayer~pi. 
write / dvd->mediaplayer~dd. 
dvd->mediaplayer~dd = 1. 
write / dvd->mediaplayer~dd.

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/18051.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论