barcodefontsoft.com

pythonhtp1_09.fm Page 314 Friday, December 14, 2001 2:01 PM in Microsoft Office Compose QR Code JIS X 0510 in Microsoft Office pythonhtp1_09.fm Page 314 Friday, December 14, 2001 2:01 PM

pythonhtp1_09.fm Page 314 Friday, December 14, 2001 2:01 PM generate, create qr codes none on microsoft office projects Code 93 Object-Oriented Programming: Inheritance 9 . CommissionW QR Code JIS X 0510 for None orker-specific earnings calculations. Method __str__ creates a string with the type and name of the employee..

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49. # Fig 9.9: Microsoft Office qr codes fig09_09.py # Creating a class hierarchy with an abstract base class.

class Employee: """Abstract base class Employee""" def __init__( self, first, last ): """Employee constructor, takes first name and last name. NOTE: Cannot create object of class Employee.""" if self.

__class__ == Employee: raise NotImplementedError, \ "Cannot create object of class Employee" self.firstName = first self.lastName = last def __str__( self ): """String representation of Employee""" return "%s %s" % ( self.

firstName, self.lastName ) def _checkPositive( self, value ): """Utility method to ensure a value is positive""" if value < 0: raise ValueError, \ "Attribute value (%s) must be positive" % value else: return value def earnings( self ): """Abstract method; derived classes must override""" raise NotImplementedError, "Cannot call abstract method" class Boss( Employee ): """Boss class, inherits from Employee""" def __init__( self, first, last, salary ): """Boss constructor, takes first and last names and salary""" Employee.__init__( self, first, last ) self.

weeklySalary = self._checkPositive( float( salary ) ) def earnings( self ): """Compute the Boss"s pay""" return self.weeklySalary Abstract class-based hierarchy.

(Part 1 of 3.). Fig. 9.9 pythonhtp1_09.fm Page 315 Friday, December 14, 2001 2:01 PM 9 . Object-Oriented Programming: Inheritance 50 51 def _ _str__( self ): 52 """String representation of Boss""" 53 54 return "%17s: %s" % ( "Boss", Employee.__str__( self ) ) 55 56 class CommissionWorker( Employee ): 57 """CommissionWorker class, inherits from Employee""" 58 59 def __init__( self, first, last, salary, commission, quantity ): 60 """CommissionWorker constructor, takes first and last names, 61 salary, commission and quantity""" 62 63 Employee.__init__( self, first, last ) 64 self.

salary = self._checkPositive( float( salary ) ) 65 self.commission = self.

_checkPositive( float( commission ) ) 66 self.quantity = self._checkPositive( quantity ) 67 68 def earnings( self ): 69 """Compute the CommissionWorker"s pay""" 70 71 return self.

salary + self.commission * self.quantity 72 73 def __str__( self ): 74 """String representation of CommissionWorker""" 75 76 return "%17s: %s" % ( "Commission Worker", 77 Employee.

__str__( self ) ) 78 79 class PieceWorker( Employee ): 80 """PieceWorker class, inherits from Employee""" 81 82 def __init__( self, first, last, wage, quantity ): 83 """PieceWorker constructor, takes first and last names, wage 84 per piece and quantity""" 85 86 Employee.__init__( self, first, last ) 87 self.wagePerPiece = self.

_checkPositive( float( wage ) ) 88 self.quantity = self._checkPositive( quantity ) 89 90 def earnings( self ): 91 """Compute PieceWorker"s pay""" 92 93 return self.

quantity * self.wagePerPiece 94 95 def __str__( self ): 96 """String representation of PieceWorker""" 97 98 return "%17s: %s" % ( "Piece Worker", 99 Employee.__str__( self) ) 100 101 class HourlyWorker( Employee ): 102 """HourlyWorker class, inherits from Employee""" Fig.

9.9 Abstract class-based hierarchy. (Part 2 of 3.

). pythonhtp1_09.fm Page 316 Friday, December 14, 2001 2:01 PM Object-Oriented Programming: Inheritance 9 . 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137. def __init_ qr codes for None _( self, first, last, wage, hours ): """HourlyWorker constructor, takes first and last names, wage per hour and hours worked""" Employee.__init__( self, first, last ) self.wage = self.

_checkPositive( float( wage ) ) self.hours = self._checkPositive( float( hours ) ) def earnings( self ): """Compute HourlyWorker"s pay""" if self.

hours <= 40: return self.wage * self.hours else: return 40 * self.

wage + ( self.hours - 40 ) *\ self.wage * 1.

5 def __str__( self ): """String representation of HourlyWorker""" return "%17s: %s" % ( "Hourly Worker", Employee.__str__( self ) ) # main program # create list of Employees employees = [ Boss( "John", "Smith", 800.00 ), CommissionWorker( "Sue", "Jones", 200.

0, 3.0, 150 ), PieceWorker( "Bob", "Lewis", 2.5, 200 ), HourlyWorker( "Karen", "Price", 13.

75, 40 ) ] # print Employee and compute earnings for employee in employees: print "%s earned $%.2f" % ( employee, employee.earnings() ).

Boss: John Smith earned $800.00 Commission Worker: Sue Jones earned $650.00 Piece Worker: Bob Lewis earned $500.

00 Hourly Worker: Karen Price earned $550.00 Fig. 9.

9 Abstract class-based hierarchy. (Part 3 of 3.).

Class Piece Microsoft Office QR-Code Worker (lines 79 99) derives from class Employee. The methods include a constructor (lines 82 88), the overridden earnings method (lines 90 93), and an __str__ method (lines 95 99). The constructor takes a first name, a last name, a wage per piece and a quantity of items produced as arguments and passes the first and last names to the Employee constructor.

Method earnings performs the PieceWorker-specific earnings calculations. Method __str__ method creates a string with the type and name of the employee..

Copyright © barcodefontsoft.com . All rights reserved.