零基础篇-基础阶段07-面向对象-继承

本文通过实例介绍了Python面向对象编程中的继承概念,包括单继承、多继承、子类重写父类方法和属性、调用父类方法以及私有属性的使用。通过煎饼果子的故事,展示了如何在类之间建立继承关系,并演示了如何在子类中访问和修改父类的属性和方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@ 面向对象-继承

一、继承的概念

Python⾯向对象的继承指的是多个类之间的所属关系,即⼦类默认继承⽗类的所有属性和⽅法,具体如
下:

# ⽗类A
class A(object):
    def __init__(self):
        self.num = 1
    def info_print(self):
        print(self.num)
# ⼦类B
class B(A):
    pass

result = B()
result.info_print() # 1

在Python中,所有的类默认继承object类,object类是顶级类或基类;其他子类叫做派生类

二、单继承

故事主线:⼀个煎饼果⼦⽼师傅,在煎饼果⼦界摸爬滚打多年,研发了⼀套精湛的摊煎饼果⼦的
技术。师⽗要把这套技术传授给他的唯⼀的最得意的徒弟
分析:徒弟是不是要继承师⽗的所有技术?

# 1.师傅类
class Master(object):
    def __init__(self):
        self.kongfu = '[古法煎饼果子配方]'

    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')

# 2.徒弟类
class Prentice(Master):
    pass

# 3.创建对象
daqiu = Prentice()

# 4.对象访问实例属性
print(daqiu.kongfu)

# 5.对象调⽤实例⽅法
daqiu.make_cake()

执行结果
在这里插入图片描述

三、多继承

故事推进:daqiu是个爱学习的好孩⼦,想学习更多的煎饼果⼦技术,于是,在百度搜索到教程,报班学习煎饼果子技术
所谓多继承意思就是⼀个类同时继承了多个⽗类

class Master(object):
    def __init__(self):
        self.kongfu = '[古法煎饼果⼦配⽅]'
    def make_cake(self):
         print(f'运⽤{self.kongfu}制作煎饼果⼦')
# 创建学校类
class School(object):
    def __init__(self):
        self.kongfu = '[百度煎饼果⼦配⽅]'
    def make_cake(self):
        print(f'运⽤{self.kongfu}制作煎饼果⼦')
class Prentice(School, Master):
    pass

daqiu = Prentice()
print(daqiu.kongfu)
daqiu.make_cake()

注意:当⼀个类有多个⽗类的时候,默认使⽤第⼀个⽗类的同名属性和⽅法

四、子类重写父类同名方法和属性

故事:daqiu掌握了师⽗和培训的技术后,⾃⼰潜⼼钻研出⾃⼰的独⻔配⽅的⼀套全新的煎饼果⼦
技术

class Master(object):
    def __init__(self):
        self.koufu = '[古法煎饼果子配方]'

    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')

class School(object):
    def __init__(self):
        self.kongfu = '[百度煎饼果子配方]'

    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')

# 独创配方
class Prentice(School,Master):
    def __init__(self):
        self.kongfu = '[独创煎饼果子配方]'

    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')

daqiu = Prentice()
print(daqiu.kongfu)
daqiu.make_cake()

print(Prentice.__mro__)

⼦类和⽗类具有同名属性和⽅法,默认使⽤⼦类的同名属性和⽅法

五、子类调用父类的同名方法和属性

故事:很多顾客都希望也能吃到古法和百度技术的煎饼果⼦

class Master(object):
    def __init__(self):
        self.kongfu = '[古法煎饼果⼦配⽅]'
    def make_cake(self):
        print(f'运⽤{self.kongfu}制作煎饼果⼦')
class School(object):
    def __init__(self):
        self.kongfu = '[百度煎饼果⼦配⽅]'

    def make_cake(self):
            print(f'运⽤{self.kongfu}制作煎饼果⼦')

class Prentice(School, Master):
    def __init__(self):
        self.kongfu = '[独创煎饼果⼦配⽅]'

    def make_cake(self):

# 如果是先调⽤了⽗类的属性和⽅法,⽗类属性会覆盖⼦类属性,故在调⽤属性前,先调⽤⾃⼰⼦类的初始化
        self.__init__()
        print(f'运⽤{self.kongfu}制作煎饼果⼦')

        # 调⽤⽗类⽅法,但是为保证调⽤到的也是⽗类的属性,必须在调⽤⽅法前调⽤⽗类的初始化
    def make_master_cake(self):
        Master.__init__(self)

        Master.make_cake(self)

    def make_school_cake(self):
        School.__init__(self)

        School.make_cake(self)

daqiu = Prentice()
daqiu.make_cake()
daqiu.make_master_cake()
daqiu.make_school_cake()
daqiu.make_cake()

六、多层继承

故事:N年后,daqiu⽼了,想要把所有技术传承给⾃⼰的徒弟

class Master(object):
     def __init__(self):
        self.kongfu = '[古法煎饼果⼦配⽅]'
     def make_cake(self):
         print(f'运⽤{self.kongfu}制作煎饼果⼦')


class School(object):
    def __init__(self):
        self.kongfu = '[⿊⻢煎饼果⼦配⽅]'

    def make_cake(self):
        print(f'运⽤{self.kongfu}制作煎饼果⼦')


class Prentice(School, Master):
    def __init__(self):
        self.kongfu = '[独创煎饼果⼦配⽅]'

    def make_cake(self):
        self.__init__()

        print(f'运⽤{self.kongfu}制作煎饼果⼦')

    def make_master_cake(self):
        Master.__init__(self)

        Master.make_cake(self)

    def make_school_cake(self):
        School.__init__(self)

        School.make_cake(self)
    # 徒孙类

class Tusun(Prentice):
    pass


xiaoqiu = Tusun()
xiaoqiu.make_cake()
xiaoqiu.make_school_cake()
xiaoqiu.make_master_cake()

七、super()调用父类方法

class Master(object):
    def __init__(self):
        self.kongfu = '[古法煎饼果⼦配⽅]'
    def make_cake(self):
        print(f'运⽤{self.kongfu}制作煎饼果⼦')
class School(Master):
    def __init__(self):
        self.kongfu = '[百度煎饼果⼦配⽅]'

    def make_cake(self):
        print(f'运⽤{self.kongfu}制作煎饼果⼦')

        # ⽅法2.1
        # super(School, self).__init__()
        # super(School, self).make_cake()
        # ⽅法2.2
        super().__init__()
        super().make_cake()

class Prentice(School):
    def __init__(self):
        self.kongfu = '[独创煎饼果⼦技术]'
    def make_cake(self):
        self.__init__()
        print(f'运⽤{self.kongfu}制作煎饼果⼦')
 # ⼦类调⽤⽗类的同名⽅法和属性:把⽗类的同名属性和⽅法再次封装
    def make_master_cake(self):
        Master.__init__(self)
        Master.make_cake(self)
    def make_school_cake(self):
        School.__init__(self)
        School.make_cake(self)

# ⼀次性调⽤⽗类的同名属性和⽅法
    def make_old_cake(self):
        #⽅法⼀:代码冗余;⽗类类名如果变化,这⾥代码需要频繁修改
        # Master.__init__(self)
        # Master.make_cake(self)
        # School.__init__(self)
        # School.make_cake(self)
        # ⽅法⼆: super()
        # ⽅法2.1 super(当前类名, self).函数()
        # super(Prentice, self).__init__()
        # super(Prentice, self).make_cake()
        # ⽅法2.2 super().函数()
        super().__init__()
        super().make_cake()

daqiu = Prentice()
daqiu.make_old_cake()

注意:使⽤super() 可以⾃动查找⽗类。调⽤顺序遵循 mro 类属性的顺序。⽐较适合单继承
使⽤

八、私有属性

8.1 定义私有属性和方法

在Python中,可以为实例属性和⽅法设置私有权限,即设置某个实例属性或实例⽅法不继承给⼦类
设置私有权限的⽅法:在属性名和⽅法名 前⾯ 加上两个下划线 __。

class Master(object):
    def __int__(self):
        self.kongfu = '[古法煎饼果子配方]'

    def make_cake(self):
        print(f'运用{self.kongfu}制作的煎饼果子')

class School(object):
    def __int__(self):
        self.kongfu = '[百度煎饼果子配方]'

    def make_cake(self):
        print(f'运用{self.kongfu}制作的煎饼果子')

class Prentice(School,Master):
    def __int__(self):
        self.kongfu = '[独创煎饼果子配方]'

        # 定义私有属性
        self._money = 20000
        # 定义私有方法
    def _info_print(self):
        print(self.kongfu)
        print(self._money)

    def make_cake(self):
        self.__init__()
        print(f'运⽤{self.kongfu}制作煎饼果⼦')

    def make_master_cake(self):
        Master.__init__(self)

        Master.make_cake(self)

    def make_school_cake(self):
        School.__init__(self)

        School.make_cake(self)

# 徒孙类
class Tusun(Prentice):
    pass

daqiu = Prentice()
# 对象不能访问私有属性和私有方法
# print(daqiu._money)
# daqiu._info_print()

xiaoqu = Tusun()
xiaoqu.__info_print()

注意:私有属性和私有⽅法只能在类⾥⾯访问和修改

8.2 获取和修改私有属性值

在Python中,⼀般定义函数名 get_xx ⽤来获取私有属性,定义 set_xx ⽤来修改私有属性值

class Master(object):
    def __int__(self):
        self.kongfu = '[古法煎饼果子配方]'

    def make_cake(self):
        print(f'运用{self.kongfu}制作的煎饼果子')

class School(object):
    def __int__(self):
        self.kongfu = '[百度煎饼果子配方]'

    def make_cake(self):
        print(f'运用{self.kongfu}制作的煎饼果子')

class Prentice(School,Master):
    def __int__(self):
        self.kongfu = '[独创煎饼果子配方]'
        self._money = 20000

    # 获取私有属性
    def get_money(self):
        return self._money

    # 修改私有属性
    def set_money(self):
        self._money = 500

    def __info_print(self):
        print(self.kongfu)
        print(self.__money)

    def make_cake(self):
        self.__init__()
        print(f'运⽤{self.kongfu}制作煎饼果⼦')

    def make_master_cake(self):
        Master.__init__(self)
        Master.make_cake(self)

    def make_school_cake(self):
        School.__init__(self)
        School.make_cake(self)

# 徒孙类
class Tusun(Prentice):
   pass
daqiu = Prentice()
xiaoqiu = Tusun()
# 调⽤get_money函数获取私有属性money的值
print(xiaoqiu.get_money())
# 调⽤set_money函数修改私有属性money的值
xiaoqiu.set_money()
print(xiaoqiu.get_money())


初学尚浅,请多关照

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值