当前位置:网站首页>ABAP-CL_OBJECT_COLLECTION工具类
ABAP-CL_OBJECT_COLLECTION工具类
2022-06-27 17:57:00 【Tab Zhu】
Title
ABAP-CL_OBJECT_COLLECTION
本文参考Jerry Wang的程序学习了使用abap来实现java中ArrayList的功能,参考文章SAP ABAP里存在Java List这种集合工具类么?CL_OBJECT_COLLECTION了解一下
一
代码
代码我是写在了本地,以便更容易看懂,主要用到了类接口和多态的思想,来避免重复造轮子。
REPORT ztest_polymorphism.
"定义接口
INTERFACE zif_shape. METHODS get_area RETURNING VALUE(rv_result) TYPE float. ENDINTERFACE. "定义计算圆面积 CLASS zcl_circle DEFINITION. PUBLIC SECTION. INTERFACES zif_shape. METHODS constructor IMPORTING !iv_radius TYPE float ."构造器,输入半径 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. "定义计算矩形面积 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. "定义计算三角形面积 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,"定义一个内表来存放不同的实例 lv_result TYPE float.
START-OF-SELECTION. "借助CL_OBJECT_COLLECTION, 采取面向对象编程里多态(Polymorphism)的思路 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( )."获取了一个迭代器 WHILE lo_iterator->has_next( )."CL_OBJECT_COLLECTION_ITERATOR DATA(lo_shape) = CAST zif_shape( lo_iterator->get_next( ) )."根据不同的类,返回值 lv_result = lv_result + lo_shape->get_area( ). ENDWHILE. WRITE:/ lv_result."圆,三角形,长方形的合计值 "这里可以实现将不同的实例加入到LT_SHAPE内表中,
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."圆,三角形,长方形的合计值
边栏推荐
- (LC)46. 全排列
- Informatics Orsay all in one 1335: [example 2-4] connected block
- Memoirs of actual combat: breaking the border from webshell
- 1027 Colors in Mars
- Is it safe to buy stocks online and open an account?
- NVIDIA Clara-AGX-Developer-Kit installation
- Leetcode 989. 数组形式的整数加法(简单)
- Pyhton爬取百度文库文字写入word文档
- 作用域-Number和String的常用Api(方法)
- 如何封装调用一个库
猜你喜欢

What is ssr/ssg/isr? How do I host them on AWS?

Core dynamic Lianke rushes to the scientific innovation board: with an annual revenue of 170million yuan, Beifang Electronics Institute and Zhongcheng venture capital are shareholders

A simple calculation method of vanishing point

Mathematical derivation from perceptron to feedforward neural network

工作流自动化 低代码是关键

从感知机到前馈神经网络的数学推导

New Zhongda chongci scientific and Technological Innovation Board: annual revenue of 284million and proposed fund-raising of 557million

MySQL表的增删改查(基础)

Jinyuan's high-end IPO was terminated: it was planned to raise 750million Rushan assets and Liyang industrial investment were shareholders

SQL Server - Window Function - 解决连续N条记录过滤问题
随机推荐
binder hwbinder vndbinder
MySQL初学者福利
redis集群系列二
使用MySqlBulkLoader批量插入数据
Informatics Orsay all in one 1335: [example 2-4] connected block
What is ICMP? What is the relationship between Ping and ICMP?
Market status and development prospect forecast of global handheld ventilator industry in 2022
金源高端IPO被终止:曾拟募资7.5亿 儒杉资产与溧阳产投是股东
binder hwbinder vndbinder
深度学习和神经网络的介绍
使用logrotate对宝塔的网站日志进行自动切割
Is the account opening QR code given by CICC securities manager safe? Who can I open an account with?
Market status and development prospect forecast of global tetramethylammonium hydroxide developer industry in 2022
Seven phases of CMS implementation
实战回忆录:从Webshell开始突破边界
CDGA|交通行业做好数字化转型的核心是什么?
NVIDIA Clara-AGX-Developer-Kit installation
在线文本按行批量反转工具
mime.type文件内容
网络传输是怎么工作的 -- 详解 OSI 模型