首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

菜鸟一个课本实例的有关问题

2012-02-17 
初学者一个课本实例的问题请教坛子里的高手们,本人最近在学习Python,做实例做到如下一个,但是编译总是说抽

初学者一个课本实例的问题
请教坛子里的高手们,本人最近在学习Python,做实例做到如下一个,但是编译总是说抽象方法不能调用,不知道问题处在哪里,请大家帮我看看,谢谢!


'''
EmployeeModule.py
'''
class Employee:
  """ """
   
  def __init__(self,first,last):
  """
  """

  if self.__class__==Employee:
  raise NotImplementedError,"Cannot create object of class Employee"
   
   
  self.firstName=first
  self.lastName=last
   
  def __str__(self):
  """ """
   
  return "s% %s"(self.firstName,self.lastName)
   
  def _checkPositive(self,value):
  """ """
   
  if value<0:
  raise ValueError,"Attribute value (%s) must be positive" % value
  else:
  return value
   
  def earnings(self):
  """ """
   
  raise NotImplementedError,"Cannot call abstract method"

#===============================================

'''
BossModule.py
'''

from EmployeeModule import *

class Boss(Employee):
  def __init__(self,first,last,salary):
  Employee.__init__(self,first,last)
  self.weekSalary=self._checkPositive(float(salary))
   
  def eranings(self):
  return self.weekSalary
   
  def __str__(self):
  return "%17s,%s"%("Boss",Employee.__str__(self))

#======================================

'''
CommissionWorkerModule.py
'''
from EmployeeModule import *

class CommissionWorker(Employee):
  def __init__(self,first,last,salary,commission,quantity):
  Employee.__init__(self,first,last)
  self.weekSalary=self._checkPositive(float(salary))
  self.commission=self._checkPositive(float(commission))
  self.quantity=self._checkPositive(quantity)
   
  def eranings(self):
  return self.salary+self.commission*self.quantity
   
  def __str__(self):
  return "%17s,%s"%("Commission Worker",Employee.__str__(self))

#==================================================
'''
HourlyWorkerModule.py
'''

from EmployeeModule import *
class HourlyWorker(Employee):
  def __init__(self,first,last,wage,hours):
  Employee.__init__(self, first, last)
  self.wage=self._checkPositive(float(wage))
  self.hours=self._checkPositive(float(hours))
   
  def earnings(self):
  if self.hours<=40:
  return self.wage*self.hours
  else:
  return 40*self.wage+(self.hours-40)*self.wage*1.5
   
  def __str__(self):
  return "%17s,%s"%("Hourly Worker",Employee.__str__(self))

#==================================================
'''
Created on 2011-12-15

@author: peter
'''
from EmployeeModule import *

class PieceWorker(Employee):
  def __init__(self,first,last,wage,quantity):
  Employee.__init__(self, first, last)
  self.wagePerPiece=self._checkPositive(float(wage))
  self.quantity=self._checkPositive(quantity)


   
  def earnings(self):
  return self.quantity*self.wagePerPiece
   
  def __str__(self):
  return "%17s,%s"%("Piece Worker",Employee.__str__(self))

#============================================

'''
test.py
'''
import unittest
from BossModule import *
from CommissionWorkerModule import *
from HourlyWorkerModule import *
from PieceWorkerModule import *

employees=[Boss("Peter","Storm",800.00),
  CommissionWorker("Sue","Jones",200.0,3.0,150),
  PieceWorker("Bob","Lewis",2.5,200),
  HourlyWorker("Karen","Price",13.75,40)]
for employee in employees:
  print "%s earned $%.2f" % (employee,employee.earnings())

#====================
调试信息:

Traceback (most recent call last):
  File "/home/peter/Ubuntu One/workspace/fig09_09_abstract/src/test.py", line 17, in <module>
  print "%s earned $%.2f" % (employee,employee.earnings())
  File "/home/peter/Ubuntu One/workspace/fig09_09_abstract/src/EmployeeModule.py", line 34, in earnings
  raise NotImplementedError,"Cannot call abstract method"
NotImplementedError: Cannot call abstract method


[解决办法]
这大概是个异常的练习示例,或者在test.py里那句报错的代码17行前后用类似这样的语句捕获就可以正常运行了:
try:
print ...
except Exception,ex:
print ex
[解决办法]

探讨

你这是自己在跟自己玩吧?employee.earnings里触发了自定义异常,你把你贴的第一个文件的earnings函数里的那句触发异常的话注释,再在下面加上一句:
print 'ok'就会提示ok了

热点排行