【Python】继承与多态

简介: 【Python】继承与多态

5675222215a9419cb00b2a6bbf5c2a1f.png


一、写在前面✨


大家好!我是初心,希望我们一路走来能坚守初心!

今天跟大家分享的文章是 Python中的继承与多态 ,希望能帮助到大家!本篇文章收录于 初心Python从入门到精通 专栏。


🏠 个人主页:初心%个人主页

🧑 个人简介:大家好,我是初心,和大家共同努力

💕欢迎大家:这里是CSDN,我记录知识的地方,喜欢的话请三连,有问题请私信😘

💕 山重水复疑无路,柳暗花明又一村。 —— 陆游「游山西村」


二、继承


继承是一种创建新的类的方式,新创建的叫子类,继承的叫父类、超类、基类。继承的特点就是子类可以使用父类的属性(特征、技能)。 继承是类与类之间的关系。

继承可以减少代码冗余、提高重用性。


语法:

Class 派生类名(基类名):

例如创建student类,继承people类的代码。

class student(people):


三、多态


多态性依赖于继承。从一个父类派生出多个子类,可以使子类之间有不同的行为,这种行为称之为多态。更直白的说,就是子类重写父类的方法,使子类具有不同的方法实现。


子类与父类拥有同一个方法,子类的方法优先级高于父类,即子类覆盖父类。只要方法存在,参数正确,就可以调用。


如果在子类中定义的一个方法,其名称、返回类型及参数列表正好与父类中某个方法的名称、返回类型及参数列表相匹配,那么可以说,子类的方法重写了父类的方法。


方法重写在不同类,是实现多态的必要条件。


四、Account和FreeChecking类的实现

编程要求:补全Account和FreeChecking类的实现。

class Account:
    """An account has a balance and a holder.
    >>> a = Account('John')
    >>> a.deposit(12)
    12
    >>> a.balance
    12
    >>> a.interest
    0.02
    >>> a.time_to_retire(20)
    26
    >>> a.balance               # balance 保持不变
    12
    >>> a.withdraw(11) # 每次取款额度不超过10,超过无法取出
    "Can't withdraw that amount"
    >>> a.withdraw(10)
    2
    >>> a.withdraw(10)
    'Insufficient funds'
    """
    def __init__(self, account_holder):
        self.balance = 0
        self.holder = account_holder
    def deposit(self, amount):
        self.balance = self.balance + amount
        return self.balance
    def withdraw(self, amount):
    def time_to_retire(self, amount):
        """Return the number of years until balance would grow to amount."""
        assert self.balance > 0 and amount > 0 and self.interest > 0
class FreeChecking(Account):
    """A bank account that charges for withdrawals, but the first two are free!
    >>> ch = FreeChecking('Jack')
    >>> ch.balance = 20
    >>> ch.withdraw(100)  # 首次取款免费,不论是否成功,免费次数减少1次
    'Insufficient funds'
    >>> ch.withdraw(3)    # 第二次取款免费
    17
    >>> ch.balance
    17
    >>> ch.withdraw(3)    # 2次免费用完,开始收费
    13
    >>> ch.withdraw(3)
    9
    >>> ch2 = FreeChecking('John')
    >>> ch2.balance = 10
    >>> ch2.withdraw(3) # 首次免费
    7
    >>> ch.withdraw(3)  # 2次免费
    5
    >>> ch.withdraw(5)  # 余额不足
    'Insufficient funds'
    """
    def __init__(self, account_holder):
    def withdraw(self, amount):
import doctest
doctest.testmod()


具体实现:

class Account:
    # 此处已经略去case
    # 利率
    interest = 0.02
    # 最大取款额度
    max_amount = 10
    def __init__(self, account_holder):
        self.balance = 0
        self.holder = account_holder
        self.interest = Account.interest
    def deposit(self, amount):
        self.balance = self.balance + amount
        return self.balance
    def withdraw(self, amount):
        # 取款超过最大取款额度,不让取款
        if (amount > Account.max_amount):
            return "Can't withdraw that amount"
        # 余额不足,不让取款
        if (self.balance < amount):
            return 'Insufficient funds'
        self.balance -= amount
        return self.balance
    def time_to_retire(self, amount):
        """Return the number of years until balance would grow to amount."""
        assert self.balance > 0 and amount > 0 and self.interest > 0
        # 账户余额 = 本金+本金*(1+利息)
        amounts = self.balance
        years = 0
        while True:
            amounts += amounts * self.interest
            years += 1
            if (amounts >= amount):
                return years
class FreeChecking(Account):
  # 此处已经略去case
    # 初始账户余额
    init_balance = 20
    # 免费取款次数
    free_draw_times = 2
    # 取款手续费
    draw_fee = 1
    def __init__(self, account_holder):
        self.balance = FreeChecking.init_balance
        self.holder = account_holder
    def withdraw(self, amount):
        # 如果账户余额小于取款额度,不能取款,次数减一
        if (self.balance < amount):
            self.free_draw_times -= 1
            return 'Insufficient funds'
        # 如果免费次数大于0小于3,免费取款
        if (self.free_draw_times > 0 and self.free_draw_times < 3):
            self.balance -= amount
        # 否则收费取款
        else:
            # 如果存款比手续费加取款额度少,不让取款
            if (self.balance - (amount + FreeChecking.draw_fee) < 0):
                return 'Insufficient funds'
            # 可以取款
            self.balance -= (amount + FreeChecking.draw_fee)
        # 免费次数减一
        self.free_draw_times -= 1
        return self.balance
import doctest
doctest.testmod()


五、总结撒花😊


本文主要讲解了Python中的继承和多态的基本使用。😊

这就是今天要分享给大家的全部内容了,我们下期再见!😊

🏠 本文由初心原创,首发于CSDN博客, 博客主页:初心%🏠

🏠 我在CSDN等你哦!😍

相关文章
|
5天前
|
Python
Python中的继承:概念、用法与示例
Python中的继承:概念、用法与示例
26 0
|
5天前
|
Python
请简述Python中的继承、封装和多态的概念。
【2月更文挑战第24天】【2月更文挑战第82篇】请简述Python中的继承、封装和多态的概念。
|
5天前
|
Python
Python中的面向对象编程与继承
本文将深入探讨Python中面向对象编程的核心概念,重点讨论继承的实现原理以及在实际开发中的应用。通过详细的示例和解释,读者将能够全面理解Python中继承的使用方式和优势,为提高代码的复用性和可维护性提供有效的技术支持。
|
5天前
|
Python
Python从入门到精通:深入学习面向对象编程——2.1.2继承、封装和多态的概念
Python从入门到精通:深入学习面向对象编程——2.1.2继承、封装和多态的概念
|
5天前
|
Python
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(上)
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(上)
62 0
|
5天前
|
Python
Python继承:深入探索与实际应用
Python中的继承是OOP三大特性之一,允许子类继承父类的属性和方法,实现代码重用和扩展。子类通过`class`关键字和父类名定义,支持单继承和多继承。子类可覆盖父类方法,使用`super()`调用父类同名方法。继承在实际应用中如游戏开发,可创建类体系,提高代码复用性,实现模块化和层次化。掌握继承对于构建高效软件系统至关重要。
|
5天前
|
Python
在Python中,如何实现多态?
在Python中,如何实现多态?
21 1
|
5天前
|
Python
一篇文章带你搞懂Python中的继承和多态
一篇文章带你搞懂Python中的继承和多态
26 1
|
5天前
|
设计模式 Python
Python-继承
Python-继承
11 3
|
数据库 Python
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(下)
## 封装,继承和多态 ## 1.封装 1、满足把内容封装到某个地方,另一个地方去调用封装的内容 2、使用初始化构造方法,或者使用self获取封装的内容 ## 2.继承 子类继承父类的属性和内容
136 0