今日学习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