0133379426.pdf by Unknown

0133379426.pdf by Unknown

Author:Unknown
Language: eng
Format: epub
ISBN: 0133379426
Published: 2013-02-27T05:51:36+00:00


12.7 Case Study: Creating and Using Interfaces

473

12.7.5 Modifying Class SalariedEmployee for Use with IPayable

Figure 12.14 contains a modified version of class SalariedEmployee that extends Employee

and implements method GetPaymentAmount. This version of SalariedEmployee is identical

to that of Fig. 12.5 with the exception that the version here implements method GetPay-

mentAmount (lines 35–38) instead of method Earnings. The two methods contain the same

functionality but have different names. Recall that the IPayable version of the method has

a more general name to be applicable to possibly disparate classes. The remaining Employee

derived classes (e.g., HourlyEmployee, CommissionEmployee and BasePlusCommissionEm-

ployee) also must be modified to contain method GetPaymentAmount in place of Earnings

to reflect the fact that Employee now implements IPayable. We leave these modifications

as an exercise and use only SalariedEmployee in our test app in this section.

1

// Fig. 12.14: SalariedEmployee.cs

2

// SalariedEmployee class that extends Employee.

3

using System;

4

5

public class SalariedEmployee : Employee

6

{

7

private decimal weeklySalary;

8

9

// four-parameter constructor

10

public SalariedEmployee( string first, string last, string ssn,

11

decimal salary ) : base( first, last, ssn )

12

{

13

WeeklySalary = salary; // validate salary via property

14

} // end four-parameter SalariedEmployee constructor

15

16

// property that gets and sets salaried employee's salary

17

public decimal WeeklySalary

18

{

19

get

20

{

21

return weeklySalary;

22

} // end get

23

set

24

{

25

if ( value >= 0 ) // validation

26

weeklySalary = value;

27

else

28

throw new ArgumentOutOfRangeException( "WeeklySalary" ,

29

value, "WeeklySalary must be >= 0" );

30

} // end set

31

} // end property WeeklySalary

32

33

// calculate earnings; implement interface IPayable method

34

// that was abstract in base class Employee

35

public override decimal GetPaymentAmount()

36

{

37

return WeeklySalary;

38

} // end method GetPaymentAmount

Fig. 12.14 | SalariedEmployee class that extends Employee. (Part 1 of 2.)



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.