• ABAP接口,类,方法


    接口的定义: 关键字INTERFACE interface

    类的定义:关键字  CLASS class DEFINITION

    类的实行:关键字  CLASS class IMPLEMENTATION.

    子类的定义:关键字  CLASS subclass DEFINITION INHERITING FROM superclass.

    方法的定义:关键字  METHOD method.(方法定义在类中,并在类中执行)

    公共变量的定义:关键字 PUBLIC SECTION. data:

    保护变量的定义:关键字 PROTECTED SECTION. data:

    私有变量的定义:关键字 PRIVATE SECTION. data:

    上面类定义和执行必须同时存在。定义是依赖于实现的。

    也就是说 CLASS class DEFINITION 和 CLASS class IMPLEMENTATION. 必须同时存在   

    code:

    REPORT demo_abap_objects.

    *---------------------------------------------------------------------*
    *       Global Selection Screen
    *---------------------------------------------------------------------*

    "定义一个窗口
    SELECTION-SCREEN BEGIN OF SCREEN 100 AS WINDOW  TITLE text-100.
    PARAMETERS: button1 RADIOBUTTON GROUP grp,
                button2 RADIOBUTTON GROUP grp,
                button3 RADIOBUTTON GROUP grp,
                button4 RADIOBUTTON GROUP grp.
    SELECTION-SCREEN END OF SCREEN 100.

    *---------------------------------------------------------------------*
    *       INTERFACE status
    *---------------------------------------------------------------------*
    *       接口定义                                         *
    *---------------------------------------------------------------------*
    INTERFACE status.
      METHODS write.
    ENDINTERFACE.

    *---------------------------------------------------------------------*
    *       CLASS vessel DEFINITION
    *---------------------------------------------------------------------*
    *       超类定义                                        *
    *---------------------------------------------------------------------*
    CLASS vessel DEFINITION.
      PUBLIC SECTION.

    "定义方法
        METHODS: constructor,
                 drive IMPORTING speed_up TYPE i,
                 get_id RETURNING value(idTYPE i.
      PROTECTED SECTION.
        DATA: speed TYPE i,
              max_speed TYPE i VALUE 100.
      PRIVATE SECTION.
        CLASS-DATA object_count TYPE i.
        DATA id TYPE i.
    ENDCLASS.

    *---------------------------------------------------------------------*
    *       CLASS vessel IMPLEMENTATION
    *---------------------------------------------------------------------*
    *       超类执行                                   *
    *---------------------------------------------------------------------*
    CLASS vessel IMPLEMENTATION.
      METHOD constructor.
        object_count = object_count + 1.
        id = object_count.
      ENDMETHOD.
      METHOD drive.
        speed = speed + speed_up.
        IF speed > max_speed.
          speed = max_speed.
        ENDIF.
      ENDMETHOD.
      METHOD get_id.
        id = me->id.
      ENDMETHOD.
    ENDCLASS.

    *---------------------------------------------------------------------*
    *       CLASS ship DEFINITION
    *---------------------------------------------------------------------*
    *       子类定义                                           *
    *---------------------------------------------------------------------*
    CLASS ship DEFINITION INHERITING FROM vessel.
      PUBLIC SECTION.
        INTERFACES status.
        DATA name TYPE string READ-ONLY.
        METHODS: constructor IMPORTING name TYPE string,
                 drive REDEFINITION.
        EVENTS emergency_call.
    ENDCLASS.

    *---------------------------------------------------------------------*
    *       CLASS ship IMPLEMENTATION
    *---------------------------------------------------------------------*
    *      子类执行                                       *
    *---------------------------------------------------------------------*
    CLASS ship IMPLEMENTATION.
      METHOD constructor.
        CALL METHOD super->constructor.
        max_speed = 30.
        me->name = name.
      ENDMETHOD.
      METHOD status~write.
        DATA id.
        id = me->get_id( ).
        WRITE: / name, 'is vessel'id,
                            'and has speed', speed.
      ENDMETHOD.
      METHOD drive.
        speed = speed + speed_up.
        IF speed > max_speed.
          max_speed = 0.
          speed = 0.
          RAISE EVENT emergency_call.
        ENDIF.
      ENDMETHOD.
    ENDCLASS.

    *---------------------------------------------------------------------*
    *       CLASS coast_guard DEFINITION
    *---------------------------------------------------------------------*
    *       事件定义                                      *
    *---------------------------------------------------------------------*
    CLASS coast_guard DEFINITION.
      PUBLIC SECTION.
        INTERFACES status.
        METHODS receive FOR EVENT emergency_call OF ship IMPORTING sender.
        ALIASES write FOR status~write.
      PRIVATE SECTION.
        DATA caller TYPE string.
    ENDCLASS.

    *---------------------------------------------------------------------*
    *       CLASS coast_guard IMPLEMENTATION
    *---------------------------------------------------------------------*
    *       事件执行                                  *
    *---------------------------------------------------------------------*
    CLASS coast_guard IMPLEMENTATION.
      METHOD status~write.
        IF caller IS INITIAL.
          WRITE: / 'Coast guard received no call'.
        ELSE.
          WRITE: / 'Coast guard received a call from', caller.
        ENDIF.
      ENDMETHOD.
      METHOD receive.
        caller = sender->name.
        CALL METHOD write.
      ENDMETHOD.
    ENDCLASS.

    *---------------------------------------------------------------------*
    *       CLASS main DEFINITION
    *---------------------------------------------------------------------*
    *       主类定义
    *---------------------------------------------------------------------*
    CLASS main DEFINITION.
      PUBLIC SECTION.
        CLASS-METHODS: start,
                       objects,
                       inheritance,
                       interfaces,
                       events.
    ENDCLASS.

    *---------------------------------------------------------------------*
    *      CLASS main IMPLEMENTATION
    *---------------------------------------------------------------------*
    *      主类执行                                      *
    *---------------------------------------------------------------------*
    CLASS main IMPLEMENTATION.

      METHOD start.
        CALL SELECTION-SCREEN 100 STARTING AT 10 3
                                    ENDING AT 42 7.
        IF sy-subrc NE 0.
          EXIT.
        ELSEIF button1 = 'X'.
          CALL METHOD objects.
        ELSEIF button2 = 'X'.
          CALL METHOD inheritance.
        ELSEIF button3 = 'X'.
          CALL METHOD interfaces.
        ELSEIF button4 = 'X'.
          CALL METHOD events.
        ENDIF.
      ENDMETHOD.

      METHOD objects.
        DATA: vessel1 TYPE REF TO vessel,
              vessel2 TYPE REF TO vessel.
        DATA: vessel_id  TYPE i.
        CREATE OBJECT: vessel1 TYPE vessel,
                       vessel2 TYPE vessel.
        CALL METHOD: vessel1->drive( 50 ),
                     vessel2->drive( 80 ).
        vessel_id = vessel1->get_id( ).
        WRITE: / 'Vessel ID is', vessel_id.
        vessel_id = vessel2->get_id( ).
        WRITE: / 'Vessel ID is', vessel_id.
      ENDMETHOD.

      METHOD inheritance.
        DATA: vessel TYPE REF TO vessel,
                ship TYPE REF TO ship.
        CREATE OBJECT ship TYPE ship EXPORTING name = 'Titanic'.
        CALL METHOD ship->drive( 20 ).
        MOVE ship TO vessel.
        CALL METHOD vessel->drive( 10 ).
        CALL METHOD ship->status~write.
      ENDMETHOD.

      METHOD interfaces.
        DATA: status_tab TYPE TABLE OF REF TO status,
              status TYPE REF TO status.
        DATA: ship TYPE REF TO ship,
              station TYPE REF TO coast_guard.
        CREATE OBJECT: ship EXPORTING name = 'Titanic'.
        APPEND ship TO status_tab.
        CREATE OBJECT station.
        APPEND station TO status_tab.
        LOOP AT status_tab INTO status.
          CALL METHOD status->write.
        ENDLOOP.
      ENDMETHOD.

      METHOD events.
        DATA: ship TYPE REF TO ship,
              station TYPE REF TO coast_guard.
        CREATE OBJECT: ship EXPORTING name = 'Titanic',
                       station.
        SET HANDLER station->receive FOR ship.
        DO 5 TIMES.
          CALL METHOD ship->drive( 10 ).
        ENDDO.
      ENDMETHOD.

    ENDCLASS.

    *---------------------------------------------------------------------*
    *      System event START-OF-SELECTION
    *---------------------------------------------------------------------*
    *      Triggered by the ABAP runtime environment automatically        *
    *---------------------------------------------------------------------*

    START-OF-SELECTION.
      CALL METHOD main=>start.

    *---------------------------------------------------------------------*

  • 相关阅读:
    iOS开发——UI基础-自定义构造方法,layoutSubviews,Xib文件,利用Xib自定义View
    iOS开发——UI基础-懒加载,plist文件,字典转模型,自定义view
    iOS开发——UI基础-按钮的创建和设置
    iOS开发——UI基础-Xcode资源拷贝
    机器学习中的数学基础_七月算法4月机器学习班第1次课程笔记
    leetcode--Merge Sorted Array
    leetcode--First Missing Positive
    leetcode--Divide two integers
    leetcode--Implement strStr()
    leetcode--Remove Element
  • 原文地址:https://www.cnblogs.com/wequst/p/1514540.html
Copyright © 2020-2023  润新知