<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

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

        來源:懂視網 責編:小采 時間:2020-11-27 14:31:35
        文檔

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

        python繼承和抽象類的實現方法:本文實例講述了python繼承和抽象類的實現方法。分享給大家供大家參考。 具體實現方法如下: 代碼如下:#!/usr/local/bin/python # Fig 9.9: fig09_09.py # Creating a class hierarchy with an abstract base class.
        推薦度:
        導讀python繼承和抽象類的實現方法:本文實例講述了python繼承和抽象類的實現方法。分享給大家供大家參考。 具體實現方法如下: 代碼如下:#!/usr/local/bin/python # Fig 9.9: fig09_09.py # Creating a class hierarchy with an abstract base class.

        本文實例講述了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程序設計有所幫助。

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

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

        python繼承和抽象類的實現方法:本文實例講述了python繼承和抽象類的實現方法。分享給大家供大家參考。 具體實現方法如下: 代碼如下:#!/usr/local/bin/python # Fig 9.9: fig09_09.py # Creating a class hierarchy with an abstract base class.
        推薦度:
        標簽: 方法 實現 python
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 青青草原亚洲视频| 永久黄网站色视频免费| 精品久久久久久亚洲综合网| 国内精品一级毛片免费看| 亚洲国产精品国产自在在线| 美女被爆羞羞网站免费| 免费va在线观看| 羞羞漫画页面免费入口欢迎你| 国产高清在线免费视频| 久久久久久久综合日本亚洲| 亚洲一卡2卡三卡4卡无卡下载| 91视频免费观看高清观看完整| 在线观看无码的免费网站| 亚洲av综合avav中文| 国产精品免费高清在线观看| 亚洲高清国产拍精品青青草原| 西西人体大胆免费视频| 亚洲精品无码久久久久AV麻豆| 伊人久久国产免费观看视频| 日本一道在线日本一道高清不卡免费| 亚洲AV综合色区无码另类小说| 99re这里有免费视频精品| 亚洲视频国产视频| 免费国产成人18在线观看| 99久久精品国产亚洲| 在线毛片片免费观看| 亚洲精品无码久久久久久久| 久久精品国产这里是免费| 久久亚洲精品成人无码网站| 久久精品网站免费观看| 亚洲一区二区三区久久久久| 在线观看免费污视频| xxxx日本在线播放免费不卡| 亚洲国产高清在线| 两性色午夜视频免费播放| 成人毛片免费观看视频| 亚洲神级电影国语版| 青青草国产免费久久久下载| 国产黄在线播放免费观看| 91嫩草私人成人亚洲影院| 国产精品无码免费视频二三区|