欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-30 09:29:34
本文实例讲述了python继承和抽象类的实现方法。分享给大家供大家参考。 具体实现方法如下: 复制代码 代码如下:#!/usr/local/bin/python #...

本文实例讲述了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程序设计有所帮助。