当前位置:网站首页>ABAP-CL_ OBJECT_ Collection tool class
ABAP-CL_ OBJECT_ Collection tool class
2022-06-27 20:37:00 【Tab Zhu】
Title
ABAP-CL_OBJECT_COLLECTION
In this paper, the reference Jerry Wang The program learned to use abap To achieve java in ArrayList The function of , Reference article SAP ABAP In existence Java List This kind of tool class ?CL_OBJECT_COLLECTION Get to know
One
Code
I wrote the code locally , To make it easier to understand , It mainly uses the idea of class interface and polymorphism , To avoid repeating the wheel .
REPORT ztest_polymorphism.
" Defining interfaces
INTERFACE zif_shape. METHODS get_area RETURNING VALUE(rv_result) TYPE float. ENDINTERFACE. " Define calculation circle area CLASS zcl_circle DEFINITION. PUBLIC SECTION. INTERFACES zif_shape. METHODS constructor IMPORTING !iv_radius TYPE float ." Constructors , Enter the radius PROTECTED SECTION. PRIVATE SECTION. DATA radius TYPE float . ENDCLASS. CLASS zcl_circle IMPLEMENTATION. METHOD constructor. me->radius = iv_radius. ENDMETHOD. METHOD zif_shape~get_area. CONSTANTS: pai TYPE float VALUE '3.14'. rv_result = pai * radius * radius. ENDMETHOD. ENDCLASS. " Define calculation rectangle area CLASS zcl_rectangle DEFINITION. PUBLIC SECTION. INTERFACES zif_shape. METHODS constructor IMPORTING !iv_height TYPE float !iv_width TYPE float . PROTECTED SECTION. PRIVATE SECTION. DATA height TYPE float . DATA width TYPE float . ENDCLASS. CLASS zcl_rectangle IMPLEMENTATION. METHOD constructor. height = iv_height. width = iv_width. ENDMETHOD. METHOD zif_shape~get_area. rv_result = width * height. ENDMETHOD. ENDCLASS. " Define calculation triangle area CLASS zcl_triangle DEFINITION. PUBLIC SECTION. INTERFACES zif_shape. METHODS constructor IMPORTING !iv_side TYPE float !iv_height TYPE float . PROTECTED SECTION. PRIVATE SECTION. DATA side TYPE float . DATA height TYPE float . ENDCLASS. CLASS zcl_triangle IMPLEMENTATION. METHOD constructor. side = iv_side. height = iv_height. ENDMETHOD. METHOD zif_shape~get_area. rv_result = ( 1 / 2 ) * side * height. ENDMETHOD. ENDCLASS.
TYPES: BEGIN OF ty_shape, shape TYPE REF TO object, END OF ty_shape. TYPES: tt_shape TYPE STANDARD TABLE OF ty_shape. DATA: lt_shape TYPE tt_shape," Define an inner table to store different instances lv_result TYPE float.
START-OF-SELECTION. " With the help of CL_OBJECT_COLLECTION, Adopt polymorphism in object-oriented programming (Polymorphism) The idea of DATA(lo_container) = NEW cl_object_collection( ). DATA(lo_circle) = NEW zcl_circle( 1 ). lo_container->add( lo_circle )." DATA(lo_rectangle) = NEW zcl_rectangle( iv_width = 1 iv_height = 2 ). lo_container->add( lo_rectangle ). DATA(lo_triangle) = NEW zcl_triangle( iv_height = 2 iv_side = 3 ). lo_container->add( lo_triangle ). DATA(lo_iterator) = lo_container->get_iterator( )." Got an iterator WHILE lo_iterator->has_next( )."CL_OBJECT_COLLECTION_ITERATOR DATA(lo_shape) = CAST zif_shape( lo_iterator->get_next( ) )." According to different classes , Return value lv_result = lv_result + lo_shape->get_area( ). ENDWHILE. WRITE:/ lv_result." round , triangle , Total value of rectangle " Here you can add different instances to LT_SHAPE In the inner table ,
DATA(entry) = VALUE ty_shape( shape = lo_circle ). APPEND entry TO lt_shape. entry = VALUE ty_shape( shape = lo_rectangle ). APPEND entry TO lt_shape. entry = VALUE ty_shape( shape = lo_iterator ). APPEND entry TO lt_shape. CLEAR lv_result.
LOOP AT lt_shape REFERENCE INTO DATA(lr_shape). lo_shape = CAST zif_shape( lr_shape->shape ). lv_result = lv_result + lo_shape->get_area( ). ENDLOOP. WRITE:/ lv_result." round , triangle , Total value of rectangle
边栏推荐
- CocosCreator播放音频并同步进度
- 键入网址到网页显示,期间发生了什么?
- [STL programming] [common competition] [Part 3]
- I haven't thought about the source for some time. After upgrading to the latest version 24, the data encryption problem is repeatedly displayed
- Eval function, global, local variables
- Paste source layer and history layer of data warehouse system
- 使用MySqlBulkLoader批量插入数据
- On the drawing skills of my writing career
- Yyds dry goods counting SQL sub query
- The meta universe virtual digital human is closer to us | Sinovel interaction
猜你喜欢

CSDN skill tree experience and product analysis (1)

可观测,才可靠:云上自动化运维CloudOps系列沙龙 第一弹

Pointers and structs

【STL编程】【竞赛常用】【part 2】

OpenSSL client programming: SSL session failure caused by an obscure function

Installation and configuration of grayog new generation log collection early warning system

429- binary tree (108. convert the ordered array into a binary search tree, 538. convert the binary search tree into an accumulation tree, 106. construct a binary tree from the middle order and post o

Redis cluster

Data intelligence enters the "deep water area", and data governance is the key

openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题
随机推荐
eval函数,全局、本地变量
muduo
Type the URL to the web page display. What happened during this period?
安装NFS服务
数据库索引
Record a failure caused by a custom redis distributed lock
SQL报了一个不常见的错误,让新来的实习生懵了
When developing digital collections, how should cultural and Museum institutions grasp the scale of public welfare and Commerce? How to ensure the security of cultural relics data?
在开发数字藏品时,文博机构该如何把握公益和商业的尺度?如何确保文物数据的安全?
数据库优化
Redis cluster Series II
Summary of redis big key problem handling
如何降低用户关注的非必要页面的权重传递?
Bit. Store: long bear market, stable stacking products may become the main theme
Redis persistence
智联招聘的基于 Nebula Graph 的推荐实践分享
Redis cluster
可观测,才可靠:云上自动化运维CloudOps系列沙龙 第一弹
Question brushing record: easy array (continuously updated)
Web APLS phase - Section 14 - local storage