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

ABAP语言类的例子,帮助大家理解,后面会有练习题自己敲下代码影响会更深刻,如有疑问或者文字有问题请联系我。时间比较匆忙,后续会持续更新修改。

*&---------------------------------------------------------------------*
*& Report ZJOB_YLS_CLASS
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zjob_yls_class.

*定义类中的项目
CLASS lcl_compt DEFINITION.
  PUBLIC SECTION.
*实例属性
    DATA gv_char TYPE c LENGTH 20 VALUE '实例属性'.
    DATA gv_concatenate TYPE c LENGTH 50.

*静态属性

    CLASS-DATA gv_static TYPE c LENGTH 20 VALUE '静态属性'.
*实例方法
    TYPES gty_char20(20) TYPE c.
    TYPES gty_char5(5) TYPE c.
    METHODS write_char.
    METHODS set_value IMPORTING i_value TYPE gty_char20.  "对change 变量赋值 传入这个i_value形式参数
    METHODS get_value EXPORTING e_value TYPE gty_char20.        "获取change变量的值 传出这个e_value形式参数

    METHODS con_value IMPORTING i_value1 TYPE gty_char5 OPTIONAL "可选参数
                                i_value2 TYPE gty_char5
                                i_value3 TYPE gty_char5
                      EXPORTING e_subrc  TYPE c
                                e_back   TYPE c." 多个参数

    METHODS ret_value IMPORTING i_value1        TYPE gty_char5 OPTIONAL "可选参数
                                i_value2        TYPE gty_char5
                                i_value3        TYPE gty_char5
                      RETURNING VALUE(e_concat) TYPE gty_char20."不能是C 必须要完整类型 type


    CONSTANTS gv_write TYPE c LENGTH 20 VALUE 'AAAAA'.
*静态方法
    CLASS-METHODS write_static.
  PROTECTED SECTION. "设计继承
    CLASS-DATA gv_super    TYPE c LENGTH 20 VALUE  'super class'.

  PRIVATE SECTION.
    CLASS-DATA gv_private  TYPE c LENGTH 10 VALUE '静态私有属性'.
    CLASS-DATA gv_change   TYPE c LENGTH 20.

ENDCLASS.

CLASS lcl_sub DEFINITION INHERITING FROM lcl_compt.

ENDCLASS.

*定义类中的方法内容

CLASS lcl_compt IMPLEMENTATION.

  METHOD write_char.
    WRITE:/ '实例方法',gv_char.

  ENDMETHOD.

  METHOD write_static.
    WRITE:/ '静态方法' , gv_static.
  ENDMETHOD.

  METHOD set_value.
    gv_change = i_value.
  ENDMETHOD.

  METHOD get_value.
    e_value   = gv_change.
  ENDMETHOD.

  METHOD con_value.
    CONCATENATE i_value1 i_value2 i_value3 INTO gv_concatenate.
    IF sy-subrc = 0.
      e_subrc   = 'S'.
      e_back    = gv_concatenate.
    ELSE.
      e_subrc   = 'E'.
    ENDIF.
  ENDMETHOD.

  METHOD ret_value.
    CONCATENATE i_value1 i_value2 i_value3 INTO gv_concatenate.
    IF sy-subrc = 0.
      e_concat  = gv_concatenate.
    ENDIF.

  ENDMETHOD.
ENDCLASS.


CLASS lcl_sub DEFINITION INHERITING FROM lcl_compt.
  PUBLIC SECTION.
    METHODS write_super.
ENDCLASS.


CLASS lcl_sub IMPLEMENTATION.
  METHOD write_super.
    WRITE:/ 'PUBLIC SECTION' , gv_char,gv_static.
    WRITE:/ 'PROTECT SETION' , gv_super.
*    WRITE:/ 'PRIVATE SETION' , gv_private.  私有的不行
  ENDMETHOD.
ENDCLASS.



*声明一个对象
DATA lcl_object   TYPE REF TO lcl_compt.
DATA lcl_object_2 TYPE REF TO lcl_compt.

START-OF-SELECTION.

  CREATE OBJECT lcl_object. "实例化
  CREATE OBJECT lcl_object_2. "实例化
*实例属性在不同实例之间是互相独立的
  WRITE:/ '访问实例' , lcl_object->gv_char.

  lcl_object->gv_char = '更换后的char值'.
  CALL METHOD lcl_object->write_char.


  CALL METHOD lcl_object_2->write_char.


  "静态属性 不依附实例

  WRITE:/'访问静态属性:' , lcl_compt=>gv_static.
  lcl_compt=>gv_static = '更改静态属性的值'.
  CALL METHOD lcl_compt=>write_static.

  CALL METHOD lcl_compt=>write_static.



*带有参数的方法调用
  CALL METHOD lcl_object->set_value
    EXPORTING
      i_value = '赋值char变量'.

  DATA gv_data TYPE lcl_object->gty_char20.
  CALL METHOD lcl_object->get_value
    IMPORTING
      e_value = gv_data.

  WRITE:/ gv_data.
*method 方法一个是importing 一个是receing
  DATA gv_subrc TYPE c LENGTH 50.
  CALL METHOD lcl_object->con_value
    EXPORTING
      i_value1 = 'china'
      i_value2 = 'SH'
      i_value3 = 'GREAT'
    IMPORTING
      e_subrc  = gv_subrc
      e_back   = gv_data.
  WRITE: / gv_subrc,
         / gv_data.


  CALL METHOD lcl_object->ret_value
    EXPORTING
      i_value1 = 'china'
      i_value2 = 'SH'
      i_value3 = 'GREAT'
    RECEIVING
      e_concat = gv_data.
  WRITE:  / gv_data.


  "private protected 常量 访问不了
  WRITE:/ lcl_compt=>gv_write.
  WRITE:/ lcl_object->gv_write.
发布了8 篇原创文章 · 获赞 0 · 访问量 760
展开阅读全文

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

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

分享到微信朋友圈

×

扫一扫,手机浏览