亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python繼承和抽象類的實現方法

 更新時間:2015年01月14日 14:40:48   投稿:shichen2014  
這篇文章主要介紹了python繼承和抽象類的實現方法,實例分析了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程序設計有所幫助。

相關文章

  • Pycharm2020.1安裝中文語言插件的詳細教程(不需要漢化)

    Pycharm2020.1安裝中文語言插件的詳細教程(不需要漢化)

    這篇文章主要介紹了Pycharm2020.1安裝中文語言插件的詳細教程,不需要漢化,本文給大家分享三種方法,在這小編推薦使用方法二,具體內容詳情大家跟隨小編一起看看吧
    2020-08-08
  • numpy.insert()的具體使用方法

    numpy.insert()的具體使用方法

    本文主要介紹了numpy.insert()的具體使用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-02-02
  • Python實現子類調用父類的初始化實例

    Python實現子類調用父類的初始化實例

    這篇文章主要介紹了Python實現子類調用父類的初始化實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • PyTorch基礎之torch.nn.CrossEntropyLoss交叉熵損失

    PyTorch基礎之torch.nn.CrossEntropyLoss交叉熵損失

    這篇文章主要介紹了PyTorch基礎之torch.nn.CrossEntropyLoss交叉熵損失講解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • python 實現return返回多個值

    python 實現return返回多個值

    今天小編就為大家分享一篇python 實現return返回多個值,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • python pandas dataframe 按列或者按行合并的方法

    python pandas dataframe 按列或者按行合并的方法

    下面小編就為大家分享一篇python pandas dataframe 按列或者按行合并的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • python框架django基礎指南

    python框架django基礎指南

    本文給大家匯總介紹了下python的MVC框架django的一些基礎知識,非常的簡單實用,希望對大家學習django能夠有所幫助。
    2016-09-09
  • Python迭代用法實例教程

    Python迭代用法實例教程

    這篇文章主要介紹了Python迭代用法,包括了迭代的定義及具體用法,是一個非常實用的技巧,需要的朋友可以參考下
    2014-09-09
  • Python腳本實現隨機數據生成自由詳解

    Python腳本實現隨機數據生成自由詳解

    這篇文章主要為大家詳細介紹了Python如何通過腳本實現隨機數據生成自由,文中的示例代碼講解詳細,感興趣的小伙伴快跟隨小編一起學習一下吧
    2023-12-12
  • 在Python 中同一個類兩個函數間變量的調用方法

    在Python 中同一個類兩個函數間變量的調用方法

    今天小編就為大家分享一篇在Python 中同一個類兩個函數間變量的調用方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01

最新評論