当前位置:网站首页>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
边栏推荐
- Practice of combining rook CEPH and rainbow, a cloud native storage solution
- Database optimization
- Pfsense plus22.01 Chinese customized version release
- 从指令交读掌握函数调用堆栈详细过程
- Question brushing record: easy array (continuously updated)
- What is a low code development platform? Why is it so hot now?
- SQL reported an unusual error, which confused the new interns
- 安装NFS服务
- Redis 大 key 问题处理总结
- Summary of redis big key problem handling
猜你喜欢

database engine

Pointers and structs

Grasp the detailed procedure of function call stack from instruction reading

NVIDIA three piece environment configuration

Character interception triplets of data warehouse: substrb, substr, substring

NVIDIA三件套环境配置

探秘GaussDB,听听客户和伙伴怎么说

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

Database lock problem

Csdn Skills Tree use Experience and Product Analysis (1)
随机推荐
Database log
MongoDB简介及典型应用场景
Common shell script commands (III)
Summary of redis big key problem handling
Bit. Store: long bear market, stable stacking products may become the main theme
ABAP essays - interview memories hope that everyone's demand will not increase and the number of people will soar
主键选择选择自增还是序列?
【STL编程】【竞赛常用】【part 3】
Type the URL to the web page display. What happened during this period?
Record a failure caused by a custom redis distributed lock
SQL报了一个不常见的错误,让新来的实习生懵了
[array]bm99 clockwise rotation matrix - simple
On the drawing skills of my writing career
实现字符串MyString
最佳实践:优化Postgres查询性能(下)
[bug] there is an error uploading the picture (413 request entity too large)
Database lock problem
Explore gaussdb and listen to what customers and partners say
数智化进入“深水区”,数据治理是关键
安装NFS服务