当前位置: 移动技术网 > IT编程>脚本编程>Python > python继承和抽象类的实现方法

python继承和抽象类的实现方法

2019年03月23日  | 移动技术网IT编程  | 我要评论

贪狼棋子和松子,财经天下,支柱蓼

本文实例讲述了python继承和抽象类的实现方法。分享给大家供大家参考。

具体实现方法如下:

复制代码 代码如下:
#!/usr/local/bin/python
# 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 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
 
   def __str__(self):
      """string representation of boss"""
 
      return "%17s: %s" % ("boss", employee.__str__(self))
 
class commissionworker(employee):
   """commissionworker class, 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 commissionworker's pay"""
 
      return self.salary + self.commission * self.quantity
 
   def __str__(self):
      """string representation of commissionworker"""
 
      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, inherits 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())

输出结果如下:

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

希望本文所述对大家的python程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网