国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python繼承和抽象類的實(shí)現(xiàn)方法

2020-02-23 06:18:55
字體:
供稿:網(wǎng)友

本文實(shí)例講述了python繼承和抽象類的實(shí)現(xiàn)方法。分享給大家供大家參考。

具體實(shí)現(xiàn)方法如下:

代碼如下:#!/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):

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 黄平县| 芦溪县| 镇远县| 嘉兴市| 永川市| 启东市| 花莲县| 蓝田县| 鄂尔多斯市| 兴化市| 思南县| 云林县| 滦南县| 内丘县| 乌苏市| 阿克苏市| 北辰区| 皮山县| 黔西县| 虎林市| 赣榆县| 稻城县| 吉安县| 南开区| 桃江县| 正蓝旗| 富宁县| 扎兰屯市| 罗山县| 图片| 亳州市| 万源市| 米脂县| 南城县| 达州市| 通许县| 东安县| 筠连县| 山西省| 柯坪县| 灵丘县|