ABAP开发基础入门类、接口练习(二)

很久没有更新博客了,下面是关于abap开发类接口的练习,如有疑问地方请留言或者私信,我看到都会回复。
这是原来的题目要求,在这要求上我自己增添了些要求,更好拓展知识点。

*&---------------------------------------------------------------------*
*& Report ZJOB_YLS_CLASS_CIRCLE
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zjob_yls_class_circle.
TYPES gty_round TYPE p LENGTH 10 DECIMALS 2.
TYPES gty_char(50) TYPE c.
DATA  gv_out TYPE p LENGTH 10 DECIMALS 2.

INTERFACE inter.
  METHODS write_inter.
ENDINTERFACE.
CLASS lcl_circle DEFINITION.
  PUBLIC SECTION.

*接口
    INTERFACES inter.
    ALIASES write_interface FOR inter~write_inter. "别名


    METHODS exp_cir   IMPORTING i_r     TYPE p
                      EXPORTING e_round TYPE p .

    METHODS return_cir IMPORTING  VALUE(i_r)     TYPE gty_round
*                                 VALUE(i_pi)    TYPE gty_round
                       RETURNING  VALUE(r_round) TYPE gty_round
                       EXCEPTIONS no_zero.
*构造器   在creat object 里进行传输
    METHODS constructor IMPORTING i_char TYPE gty_char.
  PROTECTED SECTION.
    CONSTANTS cns_pi TYPE p LENGTH 10 DECIMALS 2 VALUE '3.14'.
  PRIVATE SECTION.
*    CONSTANTS cns_pi TYPE p LENGTH 10 DECIMALS 2 VALUE '3.14'. "私有的他继承不下去
ENDCLASS.

CLASS lcl_abstract DEFINITION ABSTRACT.
  PUBLIC SECTION.
    METHODS write_abstract.
    METHODS write_abstract_02 ABSTRACT. "不需要在IMPLEMENTATION实施 但是要在子类实施
ENDCLASS.

CLASS lcl_abstract IMPLEMENTATION.
  METHOD write_abstract.
    WRITE:/ '抽象类的实例方法。'.
  ENDMETHOD.
ENDCLASS.

CLASS lcl_sub_abstract DEFINITION INHERITING FROM lcl_abstract.
  PUBLIC SECTION.
    METHODS write_abstract_02 REDEFINITION.
ENDCLASS.


CLASS lcl_sub_abstract IMPLEMENTATION.
  METHOD write_abstract_02.
    WRITE:/'抽象子类的实例方法'.
  ENDMETHOD.
ENDCLASS.

CLASS lcl_sub DEFINITION INHERITING FROM lcl_circle.
  PUBLIC SECTION.
*  METHODS write_super.
    METHODS return_cir REDEFINITION. "针对父类存在的方法重定义

    METHODS calculate_circle.
ENDCLASS.



CLASS lcl_circle IMPLEMENTATION.
  METHOD constructor.
    WRITE:/ '执行了构造器函数。'.
    WRITE:/ i_char.
  ENDMETHOD.
  METHOD exp_cir.
    e_round = 2 * cns_pi * i_r.
    WRITE:/ '圆的周长e_round为:' ,e_round.
  ENDMETHOD.
  METHOD return_cir.
    IF i_r < 0.
      RAISE no_zero.
    ENDIF.
    r_round =  cns_pi * i_r * i_r.
    WRITE:/ '圆的面积r_round为:' ,r_round.
  ENDMETHOD .

*接口
  METHOD inter~write_inter.
    WRITE:/ '这是接口方法。'.
  ENDMETHOD.
ENDCLASS.

CLASS lcl_sub IMPLEMENTATION.

  METHOD return_cir.
    CALL METHOD super->return_cir
      EXPORTING
        i_r     = i_r              "一样的参数
      RECEIVING
        r_round = gv_out.
    r_round =  gv_out * i_r * 4 / 3.
    WRITE:/ '圆的体积为:' ,r_round.
  ENDMETHOD.

  METHOD calculate_circle.
    "父类中 return_cir 计算圆的周长
*   CALL METHOD me->return_cir
*   EXPORTING
*     i_r = p_r
*    RECEIVING
*    r_round = lv_round.

    "子类中 return_cir 计算圆的面积
  ENDMETHOD.
ENDCLASS.
SELECTION-SCREEN BEGIN OF BLOCK bk WITH FRAME TITLE TEXT-001.
PARAMETERS p_r TYPE p LENGTH 10 DECIMALS 2.
SELECTION-SCREEN END OF BLOCK bk.

START-OF-SELECTION.
  PERFORM frm_get_data.
*&---------------------------------------------------------------------*
*& Form FRM_GET_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_get_data .
  DATA lcl_object TYPE REF TO lcl_circle.
*  DATA lcl_object1 TYPE REF TO lcl_circle.  "测试构造函数方法 创建object的时候自动调用
*  DATA lcl_object2 TYPE REF TO lcl_circle.
  CREATE OBJECT lcl_object
    EXPORTING
      i_char = '构造函数 实例化创建传进来。'.
*  CREATE OBJECT lcl_object1.
*  CREATE OBJECT lcl_object2.
  DATA lv_cir   TYPE p LENGTH 10 DECIMALS 2 .
  DATA lv_round TYPE p LENGTH 10 DECIMALS 2 .
  CALL METHOD lcl_object->exp_cir
    EXPORTING
      i_r     = p_r
    IMPORTING
      e_round = lv_cir.

  CALL METHOD lcl_object->return_cir
    EXPORTING
      i_r     = p_r
    RECEIVING
      r_round = lv_round "returning 只能一个参数对应 所以调用可以简化
    EXCEPTIONS
      no_zero = 1.
  IF sy-subrc = 1 .
    WRITE:/ '半径不能小于等于0'.
  ENDIF.
  lv_round = lcl_object->return_cir( p_r )." 括号是传入常数 单一的  (注意 如果是选择界面输入的是负号 需要把这两行注释掉 不然会dump掉)
  WRITE:/ '这是快捷的调用方法:' , lv_round.


  DATA lcl_subobject TYPE REF TO lcl_sub.
  CREATE OBJECT lcl_subobject
    EXPORTING
      i_char = '子类的构造方法'.

  CALL METHOD lcl_subobject->return_cir
    EXPORTING
      i_r     = p_r
    RECEIVING
      r_round = gv_out.

  DATA lcl_abstract_object TYPE REF TO  lcl_sub_abstract.
  CREATE OBJECT lcl_abstract_object.
  CALL METHOD lcl_abstract_object->write_abstract_02.

*接口方法的调用
  CALL METHOD lcl_object->inter~write_inter.
  CALL METHOD lcl_object->write_interface. "别名
ENDFORM.
发布了8 篇原创文章 · 获赞 0 · 访问量 753
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览