首页 > 其他分享 >ABAP开发面向对象---类

ABAP开发面向对象---类

时间:2023-01-02 00:01:10浏览次数:43  
标签:zcl SECTION --- 面向对象 ABAP constructor ENDMETHOD TYPE METHOD

今日学习ABAP面向对象里面的类,关于构造,继承,实现。

踩坑点:类有抽象的方法,类本身也需要是抽象的,故需要在类申明里面加上ABSTRACT关键字

学习资料为B站翱翔云天老师

 1 CLASS zcl_employee DEFINITION
 2   PUBLIC
 3   ABSTRACT
 4   CREATE PUBLIC .
 5 
 6   PUBLIC SECTION.
 7     METHODS:
 8         constructor IMPORTING firstname TYPE string
 9                               lastname TYPE string,
10         get_printname RETURNING VALUE(fullname) TYPE string,
11         earnings  ABSTRACT RETURNING VALUE(earn) TYPE f,
12         print ABSTRACT.
13   PROTECTED SECTION.
14      data:first_name TYPE string,
15           last_name TYPE string.
16 
17   PRIVATE SECTION.
18 ENDCLASS.

 

练习类的结构

父类:Employee   子类:Boss,PieceWorker,SalesPerson 

 

 

 

 

Employee类

 

 

 boss类

CLASS zcl_boss IMPLEMENTATION.
    METHOD constructor.
        CALL method super->constructor
            EXPORTING
                firstname = fn
                lastname = ln.
                weekly_salary = wl.
    ENDMETHOD.

     METHOD set_weekly_salary.
        weekly_salary = ws.
    ENDMETHOD.

    METHOD earnings.
        earn = weekly_salary * 4.
    ENDMETHOD.



    METHOD print.
        write:/,'My name is:',  get_printname( ).
        write:/,'My salary is:',  me->earnings( )  DECIMALS 2 EXPONENT 0.
    ENDMETHOD.


ENDCLASS.


CLASS zcl_piece_worker DEFINITION
  PUBLIC
  INHERITING FROM zcl_employee
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    METHODS:
      constructor IMPORTING fn       TYPE string
                            ln       TYPE string
                            wage     TYPE f
                            quantity TYPE i,

      setwage IMPORTING wage TYPE f,
      setquantity IMPORTING quantity TYPE i,
      earnings REDEFINITION,
      print REDEFINITION.
  PROTECTED SECTION.
  PRIVATE SECTION.
    DATA: wagevalue     TYPE f,
          quantityValue TYPE i.

ENDCLASS.


PieceWorker类
CLASS zcl_piece_worker IMPLEMENTATION.
  METHOD constructor.
    CALL METHOD super->constructor
      EXPORTING
        firstname = fn
        lastname  = ln.
    wagevalue = wage.
    quantityValue = quantity.
  ENDMETHOD.

  METHOD setwage.
    wagevalue = wage.
  ENDMETHOD.

  METHOD setquantity.
    quantityValue = quantity.
  ENDMETHOD.

  METHOD earnings.
    earn = wagevalue * quantityValue.
  ENDMETHOD.

  METHOD print.
    WRITE:/,'my name is:', get_printname( ).
    WRITE:/,'我的收入是:',me->earnings( )  DECIMALS 2 EXPONENT 0.

  ENDMETHOD.




ENDCLASS.

 

CLASS zcl_sales DEFINITION
  PUBLIC
  INHERITING FROM zcl_employee
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    METHODS:
      constructor IMPORTING
                    fn   TYPE string
                    ln   TYPE string
                    sal  TYPE f
                    comm TYPE float
                    quan TYPE i,
      setSalary  IMPORTING sal TYPE f,
      setComm IMPORTING comm TYPE f,
      setQuantity IMPORTING qu    TYPE i,
      earnings   REDEFINITION,
      print  REDEFINITION.


  PROTECTED SECTION.
  PRIVATE SECTION.
    DATA:salary     TYPE f,
         commission TYPE f,
         quantity   TYPE i.
ENDCLASS.



SalesMan类 CLASS zcl_sales IMPLEMENTATION. METHOD constructor. CALL METHOD super->constructor EXPORTING firstname = fn lastname = ln. salary = sal. commission = comm. quantity = quan. ENDMETHOD. METHOD setSalary. salary = sal. ENDMETHOD. METHOD setComm. commission = comm. ENDMETHOD. METHOD setQuantity. quantity = qu. ENDMETHOD. METHOD earnings. earn = salary + commission * quantity. ENDMETHOD. METHOD print. WRITE:/,'my name is:', get_printname( ). WRITE:/,'我的收入是:',me->earnings( ) DECIMALS 2 EXPONENT 0. ENDMETHOD. ENDCLASS.
*&---------------------------------------------------------------------*
*& Report zaxyt_employee_demo
*&---------------------------------------------------------------------*
*& 员工类的测试程序
*&---------------------------------------------------------------------*
REPORT zaxyt_employee_demo.



主程序 DATA: o_sales TYPE REF TO zcl_sales, o_boss TYPE REF TO zcl_boss, o_emp TYPE REF TO zcl_employee. CREATE OBJECT o_sales EXPORTING fn = 'David' ln = 'Go' sal = '1222.88' comm = '0.5' quan = 15000. o_emp = o_sales. o_sales->print( ). CREATE OBJECT o_boss EXPORTING fn = '王麻子' ln = '尼古拉斯' wl = '1000'. o_emp = o_boss. o_boss->print( ).

 

标签:zcl,SECTION,---,面向对象,ABAP,constructor,ENDMETHOD,TYPE,METHOD
From: https://www.cnblogs.com/abapunion/p/17019271.html

相关文章

  • 第五章-循环控制结构
    细节switch表达式数据类型,应和case后的常量类型一致,或者可以自动转换相互比较类型,比如输入的是字符型,而常量是int。eg:intn=12; switch(n){ //错误,表达式是int而c......
  • nginx-clojure nginx clojure & java & groovy 模块
    nginx-clojure是一个nginx扩展模块,让我们可以直接运行clojure&java&groovy,还是比较强大的,支持的功能也不少我们可以直接基于jvm对于nginx进行扩展了,还是值得尝试......
  • 2022-12月读书整理
    12月读书整理:12.10认识自己,选择生活唯一不变的是改变本身。Theonlythingthatdoesn'tchangeischangeitself.12.11精要主义试图做所有“正确”的事情,追求尽善......
  • 19、前端基础-ES6---异步编排
    ......
  • Python__20-- 常见Bug
    1Bug一杯茶,一包烟,一个Bug改一天程序错误,即英文的Bug,也称为缺陷、臭虫,是指在软件运行中因为程序本身有错误而造成的功能不正常、死机、数据丢失、非正常中断等现象。早期的......
  • D - Scope --- ATCODER
    D-Scopehttps://atcoder.jp/contests/abc283/tasks/abc283_d 思路使用stack做字符串的内容分析,除了)所有的字符依次入栈,遇到(字符,则从栈顶开始依次出栈,直到第一个(也......
  • .NET周报【12月第4期 2022-12-31】
    祝大家新年快乐!国内文章『再看.NET7』数值类型https://mp.weixin.qq.com/s/ctiBMPY6Hditk81AzHSRng在C#中,有int16,用short来定义;有int32,用int定义;用int64,用long来定义......
  • C. On Number of Decompositions into Multipliers -- Codeforces
    C.OnNumberofDecompositionsintoMultipliershttps://codeforces.com/problemset/problem/397/C 思路  Codehttps://codeforces.com/contest/397/submissi......
  • HPA&&metrics-server
    27.HPA27.1Pod伸缩简介根据当前pod的负载,动态调整pod副本数量,业务高峰期自动扩容pod的副本数以尽快响应pod的请求在业务低峰期对pod进行缩容,实现降本增效的目的公有......
  • 【疑难杂症】 lanms-proper的安装问题
    问题:pip一直install不了,去pypi看可知这东西版本很老,且只支持Linux平台。解决方法:改成安装lanms-neo,这是作者专门搞的兼容Windows版本的lanms,其接口函数与lanms-proper......