python继承和抽象类的实现方法
本文实例讲述了python继承和抽象类的实现方法。分享给大家供大家参考。
具体实现方法如下:
#!/usr/local/bin/python #Fig9.9:fig09_09.py #Creatingaclasshierarchywithanabstractbaseclass. classEmployee: """AbstractbaseclassEmployee""" def__init__(self,first,last): """Employeeconstructor,takesfirstnameandlastname. NOTE:CannotcreateobjectofclassEmployee.""" ifself.__class__==Employee: raiseNotImplementedError,\ "CannotcreateobjectofclassEmployee" self.firstName=first self.lastName=last def__str__(self): """StringrepresentationofEmployee""" return"%s%s"%(self.firstName,self.lastName) def_checkPositive(self,value): """Utilitymethodtoensureavalueispositive""" ifvalue<0: raiseValueError,\ "Attributevalue(%s)mustbepositive"%value else: returnvalue defearnings(self): """Abstractmethod;derivedclassesmustoverride""" raiseNotImplementedError,"Cannotcallabstractmethod" classBoss(Employee): """Bossclass,inheritsfromEmployee""" def__init__(self,first,last,salary): """Bossconstructor,takesfirstandlastnamesandsalary""" Employee.__init__(self,first,last) self.weeklySalary=self._checkPositive(float(salary)) defearnings(self): """ComputetheBoss'spay""" returnself.weeklySalary def__str__(self): """StringrepresentationofBoss""" return"%17s:%s"%("Boss",Employee.__str__(self)) classCommissionWorker(Employee): """CommissionWorkerclass,inheritsfromEmployee""" def__init__(self,first,last,salary,commission,quantity): """CommissionWorkerconstructor,takesfirstandlastnames, salary,commissionandquantity""" Employee.__init__(self,first,last) self.salary=self._checkPositive(float(salary)) self.commission=self._checkPositive(float(commission)) self.quantity=self._checkPositive(quantity) defearnings(self): """ComputetheCommissionWorker'spay""" returnself.salary+self.commission*self.quantity def__str__(self): """StringrepresentationofCommissionWorker""" return"%17s:%s"%("CommissionWorker", Employee.__str__(self)) classPieceWorker(Employee): """PieceWorkerclass,inheritsfromEmployee""" def__init__(self,first,last,wage,quantity): """PieceWorkerconstructor,takesfirstandlastnames,wage perpieceandquantity""" Employee.__init__(self,first,last) self.wagePerPiece=self._checkPositive(float(wage)) self.quantity=self._checkPositive(quantity) defearnings(self): """ComputePieceWorker'spay""" returnself.quantity*self.wagePerPiece def__str__(self): """StringrepresentationofPieceWorker""" return"%17s:%s"%("PieceWorker", Employee.__str__(self)) classHourlyWorker(Employee): """HourlyWorkerclass,inheritsfromEmployee""" def__init__(self,first,last,wage,hours): """HourlyWorkerconstructor,takesfirstandlastnames, wageperhourandhoursworked""" Employee.__init__(self,first,last) self.wage=self._checkPositive(float(wage)) self.hours=self._checkPositive(float(hours)) defearnings(self): """ComputeHourlyWorker'spay""" ifself.hours<=40: returnself.wage*self.hours else: return40*self.wage+(self.hours-40)*\ self.wage*1.5 def__str__(self): """StringrepresentationofHourlyWorker""" return"%17s:%s"%("HourlyWorker", Employee.__str__(self)) #mainprogram #createlistofEmployees 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)] #printEmployeeandcomputeearnings foremployeeinemployees: print"%searned$%.2f"%(employee,employee.earnings())
输出结果如下:
Boss:JohnSmithearned$800.00
CommissionWorker:SueJonesearned$650.00
PieceWorker:BobLewisearned$500.00
HourlyWorker:KarenPriceearned$550.00
希望本文所述对大家的Python程序设计有所帮助。