zoukankan      html  css  js  c++  java
  • 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.
  • 相关阅读:
    硬盘参数你都懂吗?(上)-从案例引发的讨论
    Python 面试题(下)
    Python 面试题(上)
    DNS 原理入门
    从硬盘设计思想到RAID改良之道
    (转)短信vs.推送通知vs.电子邮件:app什么时候该用哪种方式来通知用户?
    (转)移动端主动推送消息原理
    (转)OpenFire源码学习之十七:HTTP Service插件
    (转)openfire插件开发(二) 基于web的插件开发
    (转)openfire插件开发(一)
  • 原文地址:https://www.cnblogs.com/miaosj/p/10338614.html
Copyright © 2011-2022 走看看