#Fig 9.9: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 earnnings(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
def __str__(self):
"""String representation of Boss"""
return "%17s:%s"%("Boss",Employee.__str__(self))
class CommissionWorker(Employee):
"""CommissionWorker c;lass, inherits from Employee"""
def __init__(self,first,last,salary,commission,quantity):
"""CommissionWorker constructor,takes first and last names,
salary,commission and quantity"""
Employee.__init__(self,first,last)
self.salary=self._checkPositive(float(salary))
self.commission=self._checkPositive(float(commission))
self.quantity=self._checkPositive(quantity)
def earnings(self):
"""Compute the CommisionWorker's pay"""
return self.salary+self.commission*self.quantity
def __str__(self):
"""Strings representation of CommisionWorker"""
return "%17s:%s"%("Commission Worker",
Employee.__str__(self))
class PieceWorker(Employee):
"""PieceWorker class,inherits from Employee"""
def __init__(self,first,last,wage,quantity):
"""PieceWorker constructor, takes first and last names,
wage per piece and quantity"""
Employee.__init__(self,first,last)
self.wagePerPiece=self._checkPositive(float(wage))
self.quantity=self._checkPositive(quantity)
def earnings(self):
"""Compute PieceWorker's pay"""
return self.quantity*self.wagePerPiece
def __str__(self):
"""String representation of PieceWorker"""
return "%17s:%s"%("Piece Worker",Employee.__str__(self))
class HourlyWorker(Employee):
"""HourlyWorker class, inhertits from Employee"""
def __init__(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())
没有合适的资源?快使用搜索试试~ 我知道了~
python编程金典的一些例子
共66个文件
py:45个
pyc:15个
gif:5个
5星 · 超过95%的资源 需积分: 10 19 下载量 92 浏览量
2013-12-15
15:15:25
上传
评论
收藏 117KB ZIP 举报
温馨提示
给出了一些关于《python编程金典》这本书的一些源代码,有一些代码可能有些改动,仅供python语言学习者一些参考。
资源推荐
资源详情
资源评论
收起资源包目录
study_example.zip (66个子文件)
study_example
fig10_07.py 1KB
NewList.py 2KB
time1.py 929B
fig10_06.py 2KB
fig09_15.py 856B
fig11_01.py 2KB
fig08_10.py 1KB
Date.pyc 2KB
time1.pyc 1KB
fig10_16.py 1KB
fig10_17.py 2KB
fig09_09.py 4KB
PointModule.pyc 1KB
Date.py 1KB
EmployeeStatic.py 2KB
TimeAccess.py 2KB
fig08_16.py 560B
fig10_21.py 3KB
fig09_13.py 947B
Time3.pyc 3KB
fig11_04.py 1KB
fig11_05.py 1KB
fig09_15.pyc 1KB
logotiny.gif 18KB
CircleModule.py 1KB
RationalNumber.py 4KB
fig11_03.py 3KB
Fig09_12.py 3KB
fig10_12.py 2KB
fig10_08.py 2KB
fig08_01.py 226B
TimeAccess.pyc 2KB
PhoneNumber.py 917B
CircleModule.pyc 2KB
travelbag.gif 9KB
NewDictionary.py 1008B
EmployeeComposition.pyc 1KB
CylinderModule.py 2KB
fig09_05.py 1KB
Time2.py 2KB
fig08_01 226B
fig08_13.py 1KB
NewList.pyc 2KB
RationalNumber.pyc 5KB
fig11_02.py 1KB
Time2.pyc 3KB
fig10_11.py 2KB
fig10_09.py 2KB
EmployeeComposition.py 1KB
buganim.gif 33KB
NewDictionary.pyc 2KB
fig10_14.py 2KB
fig10_04.py 919B
bug1.gif 19KB
PointModule.py 759B
EmployeeWithClassAttribute.pyc 1KB
fig12_01.py 646B
EmployeeWithClassAttribute.py 772B
fig09_04.py 2KB
bug2.gif 4KB
Private.py 346B
Fig09_12.pyc 3KB
fig11_07.py 1KB
Slots.py 1KB
Private.pyc 669B
Time3.py 2KB
共 66 条
- 1
资源评论
- 平静背后的哲学2015-10-06很好 对于初次学习的我很有帮助 想学习的可以下载有
- 犄角旮旯_kang2014-05-22很实用 很好
huangzhichang13
- 粉丝: 31
- 资源: 24
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功