OO ABAP Syntax

OO ABAP Syntax

Template for making a class

Delete the parts that should not be used

******************************************
* Definition part
******************************************
CLASS xxx DEFINITION.
*------------------------------
* Public section

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

PUBLIC SECTION.
    TYPES:
    DATA: 
*   Static data
    CLASS-DATA:
*   Methods
    METHODS:
*     Using the constructor to initialize parameters
       constructor    IMPORTING xxx type yyy,
                                
*     Method with parameters
      mm1 IMPORTING iii   TYPE ttt.
*     Method without parameters
      mm2.
*   Static methods
    CLASS-METHODS:  
                    
*---------------------------------------------------*
* Protected section. Also accessable by subclasses
*---------------------------------------------------
  PROTECTED SECTION.
*---------------------------------------------------
* Private section. Not accessable by subclasses
*---------------------------------------------------
  PRIVATE SECTION.
ENDCLASS.
******************************************
* Implementation part
******************************************
CLASS lcl_airplane IMPLEMENTATION.
  METHOD constructor.
  ENDMETHOD.
  METHOD mm1.
  ENDMETHOD.
  METHOD mm2.
  ENDMETHOD.
ENDCLASS.

Template for calling a class

* Create reference to class lcl_airplane
DATA: airplane1 TYPE REF TO lcl_airplane.
START-OF-SELECTION.
* Create instance using parameters in the cosntructor method
  CREATE OBJECT airplane1 exporting im_name = 'Hansemand'
                                    im_planetype = 'Boing 747'.
*  Calling a method with parameters
  CALL METHOD: airplane1->display_n_o_airplanes,
               airplane1->display_attributes.

Subclass

CLASS xxx DEFINITION INHERITING FROM yyy.

Using af class as a parameter for a method

The class LCL_AIRPLANE is used as a parameter for method add_a_new_airplane:

METHODS:
  add_a_new_airplane importing im_airplane TYPE REF to lcl_airplane.

Interfaces

In ABAP interfaces are implemented in addition to, and independently of classes. An interface only has a declaration part,
and do not have visibility sections. Components (Attributes, methods, constants, types) can be defined the same way as in classes.

·                Interfaces are listed in the definition part lof the class, and must always be in the PUBLIC SECTION.
·                Operations defined in the interface atre impemented as methods of the class. All methods of the interface
must be present in the  implementation part of the class.
·                Attributes, events, constants and types defined in the interface are automatically available to the class 
carrying out the implementation.
·                Interface components are addressed in the class by ~

Example of how to implement an interface:
INTERFACE lif_document
  DATA:          author type ref to lcl_author.
  METHODS:  print,
                     display.
ENDINTERFACE.
CLASS lcl_text_document DEFINITION.
   PUBLIC SECTION.
      INTERFACES lif_document.
      METHODS     display.
ENDCLASS.
CLASS lcl_text_document IMPLEMENTTION.
  METHOD lif_document~print.
  ENDMETHOD.
  METHOD lif_document~display
  ENDMETHOD.
  METHOD display.
  ENDMETHOD.
ENDCLASS.
REPORT zzz.
  DATA: text_doc TYPE REF TO lcl_document.
  Start-of-selection.
    CREATE OBJECT text_doc.
    CALL METHOD text_doc->lif_document~print.
    CALL METHOD text_doc->lif_document~display.
    CALL METHOD text_doc->display.

Events

For events of controls, refer to How to use controls.

·                Events can only have EXPORTING parameters
·                When an event is triggered, only those events handlers that have registered themselves 
using SET HANDLER by this point of runtime are  executed. You can register an event using 
ACTIVATION 'X' and derigert it by using ACTIVATION 'SPACE'.
Defining events:
CLASS  DEFINITION.
  EVENTS:  EXPORTING VALUE () TYPE type.
CLASS  IMPLEMENTATION.
  METHOD :
    RAISE EVENT  EXPORTING  = .
Handling events:
CLASS  DEFINITION.
  METHODS:  FOR  OF  !  IMPORTING ... SENDER.
Setting handler  
SET HANDLER  FOR  ! FOR ALL INSTANCES
                             [ACTIVATION ]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/190059/viewspace-476528/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/190059/viewspace-476528/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值