ABAP oo exercises

Source: Internet
Author: User

Reference: skysky

Continue to look at the instance to learn ABAP object-oriented programming.

In this example, we design a program with the employee's salary as the blueprint, and then step by step improve the program we write so that it has better scalability and maintainability.

The base class used in this example is the employee class. Its sub-classes include: boss class. No matter how long it takes, he always has a fixed weekly salary (Great !). Commission worker in the sales class. His income is a small part of his salary plus a certain percentage of his sales. The piece worker in the pedometer class, his income depends on the number of pieces produced.

I will list the code, which is basically a section of a class, and finally a test program for these classes.

The class diagram is as follows:

Section 1: Implementation of the employee class

*&---------------------------------------------------------------------*

* & Include zbo_oo_employee_class

*&---------------------------------------------------------------------*

* Defines a base class. This class is an abstract class and cannot be directly instantiated. It must be instantiated in an object.

Class employee definition abstract.

Public section.

Methods:

* Defines the constructor. There are two parameters: Last Name and name.

Constructor importing a type string

B type string,

* Define the getter method to get the names respectively.

Getfirstname Returning Value (FI) type string,

Getlastname Returning Value (LA) type string,

* Two abstract methods are defined. Because the income structure of various employees is different, they must be included in their subclass.

* Instantiation

Earnings Abstract

Returning Value (EARN) Type F,

Print abstract.

Private section.

* Defines the private variable, which cannot be used in the subclass.

Data:

First_name type string,

Last_name type string.

Endclass.

* Class implementation

Class employee implementation.

* Initialization class

Method constructor.

First_name =.

Last_name = B.

Endmethod.

* Implement two getter Methods

Method getfirstname.

FI = first_name.

Endmethod.

Method getlastname.

La = last_name.

Endmethod.

Endclass.

Section 2: Implementation of the boss class

*&---------------------------------------------------------------------*

* & Include zbo_oo_boss_class

*&---------------------------------------------------------------------*

* Defines the boss class, which inherits from the employee

Class boss definition inheriting from employee.

Public section.

Methods:

* Define constructors and initialize objects

Constructor importing boss_a type string

Boss_ B type string

S Type F,

* Methods for defining the boss class (setter method)

Setweeklysalary importing WS Type F,

* Implement abstract methods in the base class because the boss class has its own salary structure.

* In this section

Earnings redefinition,

Print redefinition.

Private section.

Data:

Weeklysalary Type F.

Endclass.

Class boss implementation.

Method constructor.

* Call the constructor of the parent class

Call method super-> Constructor

Exporting a = boss_a

B = boss_ B.

* Set the boss-class private instance variables based on the new input variables.

Weeklysalary = S.

Endmethod.

* Setter method implementation

Method setweeklysalary.

Weeklysalary = ws.

Endmethod.

* Defines wage and output implementation

Method earnings.

Earn = weeklysalary.

Endmethod.

Method print.

Data: first type string,

Last type string.

First = getfirstname ().

Last = me-> getlastname ().

Write:/'boss name is: ', last, first.

Endmethod.

Endclass.

Section 3: Implementation of the Commission worker class

*&---------------------------------------------------------------------*

* & Include zbo_oo_comworker_class

*&---------------------------------------------------------------------*

* Defines the Commission worker class, whose income is a small portion of basic salary plus sales.

* A certain percentage

Class commissionworker Definition

Inheriting from employee.

Public section.

Methods:

* Define Constructors

Constructor

Importing a type string

B type string

C type F

D type F

E type I,

* Define the setter and getter Methods

Setsalary

Importing S Type F,

Getsalary

Returning Value (SA) Type F,

Setcommission

Importing C Type F,

Setquantity

Importing q type I,

* Define the abstract method of the parent class

Earnings redefinition,

Print redefinition.

Private section.

* Define private instance variables

Data:

Salary Type F,

Commission Type F,

Quantity type I.

Endclass.

* Commission worker class

Class commissionworker implementation.

Method constructor.

Call method super-> Constructor

Exporting a = a B = B.

Salary = C.

Commission = D.

Quantity = E.

Endmethod.

Method setsalary.

Salary = S.

Endmethod.

Method getsalary.

Sa = salary.

Endmethod.

Method setcommission.

Commission = C.

Endmethod.

Method setquantity.

Quantity = Q.

Endmethod.

* Wage Algorithm

Method earnings.

Earn = salary + Commission * quantity.

Endmethod.

* Implementation Output Method

Method print.

Data: fi type string,

La type string.

Write:/'Commission worker :'.

FI = getfirstname ().

La = getlastname ().

Write: fi, la.

Endmethod.

Endclass.

Section 4: Implementation of the piece worker class

*&---------------------------------------------------------------------*

* & Include zbo_oo_piworker_class

*&---------------------------------------------------------------------*

* Defines the piece worker class. His salary depends on the number of pieces produced by him.

Class piworker Definition

Inheriting from employee.

Public section.

Methods:

* Define Constructors

Constructor

Importing a type string

B type string

W Type F

Q type I,

* Setter Method

Setwage

Importing W Type F,

Setquantity

Importing q type I,

* Instantiation abstract Method

Earnings redefinition,

Print redefinition.

Private section.

Data:

Wage Type F,

Quan type I.

Endclass.

Class piworker implementation.

Method constructor.

Call method super-> Constructor

Exporting a = a B = B.

Wage = W.

Quan = Q.

Endmethod.

Method setwage.

Wage = W.

Endmethod.

Method setquantity.

Quan = Q.

Endmethod.

* Calculate the salary

Method earnings.

Earn = Quan * wage.

Endmethod.

* Redefinition of the Output Method

Method print.

Data: fi type string, la type string.

Write:/'piece worker :'.

FI = getfirstname ().

La = getlastname ().

Write: fi, la.

Endmethod.

Endclass.

Section 5: Implementation of the Test Program

*&---------------------------------------------------------------------*

* & Report zbo_oo_saplink_004

*&

*&---------------------------------------------------------------------*

*&

*&

*&---------------------------------------------------------------------*

Report zbobo_oo_saplink_004.

Include zbo_oo_employee_class.

Include zbo_oo_boss_class.

Include zbo_oo_comworker_class.

Include zbo_oo_piworker_class.

* Define referenced Variables

Data:

Em_ref type ref to employee,

Bo_ref type ref to boss,

Co_ref type ref to commissionworker,

Pi_ref type ref to piworker.

Data:

Earns Type F.

Start-of-selection.

* Create a boss object

Create object bo_ref

Exporting boss_a = 'jhon'

Boss_ B = 'Smith'

S = '2014. 00 '.

* Narrowing cast

Em_ref = bo_ref.

* Output to screen

Call method em_ref-> Print.

Earns = em_ref-> earnings ().

Write:/'earned: $ ', earns decimals 2 exponent 0.

* The following code is basically the same as the above Code

Create object co_ref

Exporting a = 'sue'

B = 'Jones'

C = '2014. 00'

D = '3. 0'

E = 150.

Em_ref = co_ref.

Call method em_ref-> Print.

Earns = em_ref-> earnings ().

Write:/'earned: $ ', earns decimals 2 exponent 0.

Create object pi_ref

Exporting a = 'bob'

B = 'Lewis'

W = '2. 5'

Q = 200.

Em_ref = pi_ref.

Call method em_ref-> Print.

Earns = em_ref-> earnings ().

Write:/'earned: $ ', earns decimals 2 exponent 0.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.