SHARE
TWEET

Untitled

a guest Nov 13th, 2019 136 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. *-----------------------------------------
  2. *класс с методом умножения числа на 2
  3. CLASS lcl_example_double DEFINITION FINAL.
  4.   PUBLIC SECTION.
  5.     DATA:
  6.      n TYPE i READ-ONLY.
  7.     METHODS:
  8.      constructor IMPORTING n TYPE i,
  9.      double.
  10.  ENDCLASS.
  11.  
  12.  CLASS lcl_example_double IMPLEMENTATION.
  13.    METHOD constructor.
  14.      me->n = n.
  15.    ENDMETHOD.
  16.    METHOD double.
  17.      n = n * 2.
  18.    ENDMETHOD.
  19.  ENDCLASS.
  20.  
  21. *--------------------------------------------
  22. * класс с примером подохода процедурного и ФП стиля
  23.  CLASS lcl_example_multi DEFINITION FINAL.
  24.   PUBLIC SECTION.
  25.     METHODS:
  26.      multi
  27.           IMPORTING a TYPE i
  28.           EXPORTING n TYPE i,
  29.      multi_return
  30.           IMPORTING n TYPE i
  31.                     a TYPE i
  32.           RETURNING VALUE(return) TYPE i.
  33.  ENDCLASS.
  34.  
  35.  CLASS lcl_example_multi IMPLEMENTATION.
  36.    METHOD multi.
  37.      n = n * a.
  38.    ENDMETHOD.
  39.  
  40.    METHOD multi_return.
  41.      return = a * n.
  42.    ENDMETHOD.
  43.  ENDCLASS.
  44.  
  45. *--------------------------------
  46. *пример параметров обязательных и нет
  47.  CLASS lcl_example_parameters DEFINITION FINAL.
  48.    PUBLIC SECTION.
  49.     METHODS:
  50.       test
  51.         IMPORTING a TYPE string
  52.                   b TYPE string DEFAULT 'DEFAULT STRING'
  53.                   c TYPE string OPTIONAL
  54.         RETURNING VALUE(return) TYPE string.
  55.  ENDCLASS.
  56.  
  57.  CLASS lcl_example_parameters IMPLEMENTATION.
  58.     METHOD test.
  59.       return = | a= { a } \| b = { b } { COND #( WHEN c IS SUPPLIED THEN | c = { c } OPTIONAL | ELSE | c = empty | ) } |.
  60.     ENDMETHOD.
  61.  ENDCLASS.
  62.  
  63. *------------------------------------------
  64. * пример с наследованием и суперклассами
  65.  
  66.  CLASS lcl_beg_a DEFINITION.
  67.    PUBLIC SECTION.
  68.     METHODS:
  69.       constructor,
  70.       some_method,
  71.       test_super RETURNING VALUE(return) TYPE string.
  72.    "защищенная может наследоваться
  73.    PROTECTED SECTION.
  74.      DATA: protected_attr TYPE string.
  75.    "приватная секция не наследуется
  76.    PRIVATE SECTION.
  77.      DATA: priv_attr TYPE string VALUE 'attr from super class'.
  78.  ENDCLASS.
  79.  
  80.  CLASS lcl_beg_a IMPLEMENTATION.
  81.    METHOD constructor.
  82.      WRITE: /'call constructor superclass'.
  83.    ENDMETHOD.
  84.    METHOD some_method.
  85.      WRITE: /'call some_method superclass'.
  86.      WRITE:/ |priv_attr { priv_attr } |.
  87.      WRITE: / |this protected attr { protected_attr }|.
  88.    ENDMETHOD.
  89.  
  90.    METHOD test_super.
  91.     return = 'Hello world'.
  92.    ENDMETHOD.
  93.  ENDCLASS.
  94.  
  95.  CLASS lcl_beg_b DEFINITION INHERITING FROM lcl_beg_a.
  96.    PUBLIC SECTION.
  97.     METHODS: constructor IMPORTING a TYPE string,
  98.              some_method REDEFINITION.
  99.    PRIVATE SECTION.
  100.     DATA: priv_attr TYPE string VALUE 'attr from child class'.
  101.  ENDCLASS.
  102.  
  103.  CLASS lcl_beg_b IMPLEMENTATION.
  104.    METHOD constructor.
  105.      super->constructor( ).
  106.      WRITE: /'call constructor child'.
  107.      protected_attr = a.
  108.    ENDMETHOD.
  109.    METHOD some_method.
  110.      super->some_method( ).
  111.      WRITE: /.
  112.      WRITE: /'call some_method child'.
  113.      WRITE:/ |priv_attr { priv_attr } |.
  114.      WRITE: / |this protected attr { protected_attr }|.
  115.    ENDMETHOD.
  116.  ENDCLASS.
  117.  
  118. *----------------------------------------------------
  119. * класс отправитель
  120.  CLASS lcl_event_sender DEFINITION.
  121.    PUBLIC SECTION.
  122.     METHODS: clock.
  123.     EVENTS update_clock EXPORTING VALUE(count) TYPE i.
  124.    PRIVATE SECTION.
  125.     DATA: counter TYPE i.
  126.  ENDCLASS.
  127.  
  128.  CLASS lcl_event_sender IMPLEMENTATION.
  129.    METHOD clock.
  130.      ADD 1 to counter.
  131.      RAISE EVENT update_clock EXPORTING count = counter. "генерируем события
  132.    ENDMETHOD.
  133.  ENDCLASS.
  134. * класс обработчик
  135.  CLASS lcl_event_receive DEFINITION.
  136.    PUBLIC SECTION.
  137.     METHODS:
  138.       constructor,
  139.       event_dispatch FOR EVENT update_clock OF lcl_event_sender IMPORTING count."описываем, что метод принимает из события парметр
  140.    PRIVATE SECTION.
  141.     CLASS-DATA:
  142.       cls_counter TYPE i VALUE 0.
  143.     DATA:
  144.       current_cls_num TYPE i.
  145.  ENDCLASS.
  146.  
  147.  CLASS lcl_event_receive IMPLEMENTATION.
  148.    METHOD constructor.
  149.      ADD 1 TO lcl_event_receive=>cls_counter.
  150.      current_cls_num = lcl_event_receive=>cls_counter.
  151.      "регистрируем наш метод на событие
  152.      SET HANDLER me->event_dispatch FOR ALL INSTANCES.
  153.      WRITE:/ |Create receiver { current_cls_num }|.
  154.    ENDMETHOD.
  155.    "метод обработчик события
  156.    METHOD event_dispatch.
  157.      WRITE: / |Dispatch event in class { current_cls_num } count = { count }|.
  158.    ENDMETHOD.
  159.  ENDCLASS.
  160. *---------------------------------------------
  161. *Классы исключения
  162.  
  163. CLASS lcx_except_static DEFINITION INHERITING FROM cx_static_check.
  164. ENDCLASS.
  165.  
  166. CLASS lcx_except_dynamic DEFINITION INHERITING FROM cx_dynamic_check.
  167.   PUBLIC SECTION.
  168.     INTERFACES if_t100_message.
  169. ENDCLASS.
  170.  
  171. class lcx_except_no_check DEFINITION INHERITING FROM cx_no_check.
  172. ENDCLASS.
  173.  
  174. CLASS lcl_exception_example DEFINITION.
  175.   PUBLIC SECTION.
  176.     METHODS: test IMPORTING t TYPE c." RAISING lcx_except_static.
  177. ENDCLASS.
  178.  
  179. CLASS lcl_exception_example IMPLEMENTATION.
  180.   METHOD test.
  181.     CASE t.
  182.       WHEN 'S'.
  183. *        RAISE EXCEPTION NEW lcl_except_static( ).
  184.         RAISE EXCEPTION TYPE lcx_except_static.
  185.       WHEN 'D'.
  186.         RAISE EXCEPTION TYPE lcx_except_dynamic MESSAGE i000(zdemo_msg) .
  187.       WHEN OTHERS.
  188.         RAISE EXCEPTION TYPE lcx_except_no_check.
  189.      ENDCASE.
  190.   ENDMETHOD.
  191. ENDCLASS.
  192.  
  193. CLASS lcl_zdemo DEFINITION CREATE PRIVATE.
  194.   PUBLIC SECTION.
  195.     CLASS-METHODS:
  196.       get_instance RETURNING VALUE(ro_instance) TYPE REF TO lcl_zdemo.
  197.     METHODS:
  198.       example_double IMPORTING n TYPE i
  199.                      RETURNING VALUE(return_obj) TYPE REF TO lcl_example_double,
  200.       example_multi,
  201.       example_multi_func,
  202.       example_parameters,
  203.       example_beginner,
  204.       example_events.
  205.   PRIVATE SECTION.
  206.     CLASS-DATA :
  207.       instance TYPE REF TO lcl_zdemo.
  208. ENDCLASS.
  209.  
  210. CLASS lcl_zdemo IMPLEMENTATION.
  211.   METHOD get_instance.
  212.     IF instance IS NOT BOUND.
  213.       instance = NEW #( ).
  214.     ENDIF.
  215.     ro_instance = instance.
  216.   ENDMETHOD.
  217.  
  218.  
  219.  
  220.  
  221.  
  222.   "пример подхода как к объектной сущности
  223.   METHOD example_double.
  224.     WRITE:'Example double'.
  225.     DATA(obj) = NEW lcl_example_double( n ).
  226.     WRITE: / obj->n.
  227.     obj->double( ).
  228.     WRITE: / obj->n.
  229.   ENDMETHOD.
  230.   "пример умножения в процедурном стиле
  231.   METHOD example_multi.
  232.     WRITE:'Example multi'.
  233.     DATA(n) = 2.
  234.     NEW lcl_example_multi( )->multi( EXPORTING a = 2 IMPORTING n = n ).
  235.     WRITE: / n.
  236.   ENDMETHOD.
  237.   "пример умножения в стиле ФП
  238.   METHOD example_multi_func.
  239.     WRITE:'Example multi functional style'.
  240.     DATA(n) = 2.
  241.     WRITE: /  NEW lcl_example_multi( )->multi_return( a = 2 n = 3 ).
  242.   ENDMETHOD.
  243.   "передача обязательных не обязательных и дефолтных параметров
  244.   METHOD example_parameters.
  245.      WRITE:'Example parameters'.
  246.      WRITE: / NEW lcl_example_parameters( )->test( 'abc' ).
  247.      WRITE: / NEW lcl_example_parameters( )->test( a = 'abc' b = '123' ).
  248.      WRITE: / NEW lcl_example_parameters( )->test( a = 'abc' b = '123' c = 'not empty').
  249.   ENDMETHOD.
  250.  
  251.   METHOD example_beginner.
  252.     WRITE: 'Пример наследования, полиморфизма и суперклассы'.
  253.     DATA(obj) = NEW lcl_beg_b( 'Hello world' ).
  254.     obj->some_method( ).
  255.     WRITE: / obj->test_super( ).
  256.   ENDMETHOD.
  257.  
  258.   METHOD example_events.
  259.     WRITE: 'События и обработка их'.
  260.     DATA(obj_sender) = NEW lcl_event_sender( ).
  261.     DATA(obj_dispatch_1) = NEW lcl_event_receive( ).
  262.     DATA(obj_dispatch_2) = NEW lcl_event_receive( ).
  263.  
  264.     DO 5 TIMES.
  265.       obj_sender->clock( ).
  266.     ENDDO.
  267.   ENDMETHOD.
  268. ENDCLASS.
  269.  
  270. START-OF-SELECTION.
  271.   lcl_zdemo=>get_instance( )->example_events( ).
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top