Python面向对象编程三大特性之继承

继承

  • 继承
  • 继承的语法
  • 重写
  • 扩展
  • 引用
  • super().父类方法名
  • 参数传递

code:_6Inheritance.py

# *****************************************************
"""
object类:在不指定继承的父类时,所有类都继承自object类(系统提供)
继承:
    当一个类去继承父类,那么这个类就拥有并可使用了父类中的所有成员包括魔术方法(除了私有成员)
    父类:被其他类继承的类,也叫做基类 或者超类;一个父类可以被多个子类继承
    子类:继承其他的类,也叫派生类
    继承的意义:提高代码的重用性,建立新的类与类的关系,方便其他逻辑的操作
继承的语法:
    class 父类():
        pass
    class 子类(父类):
        pass
重写:子类继承父类后,重新定义了父类中的方法,这样子类对象调用该方法时使用的是子类自定义的方法
扩展:子类继承父类后,可以定义自己的成员属性和成员方法
引用:子类继承父类后,并不会把父类的成员复制给子类,而是引用
super().父类方法名:在子类中可以去直接调用父类定义的方法 super().父类方法名
参数传递:子类调用父类方法时,父类需要的参数要求传递。
"""


class Person:
    name = None
    sex = None

    def __init__(self,n,s):
        self.name = n
        self.sex = s

    def talk(self):
        print(f"{self.name}会说话***************")


class Singer(Person):
    level = None

    def __init__(self, n, s,l):
        super().__init__(n, s)  # 调用父类的初始化函数
        self.name = n
        self.sex = s
        self.level = l

    def talk(self):
        print(f"{self.name}说话很好听***************")
        super().talk()

    def sing(self):
        print(f"{super().name}会唱歌*************")


Jasmine = Singer("Jasmine",23,9)
Jasmine.talk()
Jasmine.sing()

运行结果:

E:\Programs_Way\Python\python.exe D:/Prj/_PythonSelf/Object_oriented_programming/_6Inheritance.py
Jasmine说话很好听***************
Jasmine会说话***************
None会唱歌*************

Process finished with exit code 0

多继承

code:_7Multipy_Inheritance

# *****************************************************************
"""
单继承:一个类继承一个父类
多继承:一个类继承多个父类
链式继承:A类继承B类,B类继承C类,C类继承D类
单继承语法格式:见_6Inheritance
多继承语法格式:
    class 父类1():
        pass
    class 父类2():
        pass
    ...
    class 子类(父类1,父类2,...):
        pass
注意:如果两个父类都有同名称的成员函数,使用super.成员函数调用成员函数时会执行继承的第一个父类的相关成员函数
"""


class father:
    def eat(self):
        print("大口吃饭~")


class mother:
    def eat(self):
        print("小口吃饭~")


class child(father,mother):
    def eat(self):
        super().eat()  # 这里会调用第一个父类的方法
        print("又哭又闹吃饭饭~")


Boly = child()
Boly.eat()

运行结果:

E:\Programs_Way\Python\python.exe D:/Prj/_PythonSelf/Object_oriented_programming/_7Multipy_Inheritance.py
大口吃饭~
又哭又闹吃饭饭~

Process finished with exit code 0

菱形继承

code:_8diamond_inheritance.py

# **********************************************************
"""
菱形继承:
    类.mro()---获取类的继承关系列表
    super()---使用super()去访问父级的方法或属性时,实际上实在用super()调用MRO(Mothod Relation Order方法关系列表)列表中的上一级中的方法。
        使用super()方法时,传递的self对象,一直是最开始调用它的对象。
    MRO规则:
    child.mro() = [<class '__main__.child'>, <class '__main__.father'>, <class '__main__.mother'>, <class '__main__.human'>, <class 'object'>]
        1.子类永远在父类前面
        2.同一等级的类,按照子类中的继承顺序摆放
        3.先子类,后父类的顺序原则,最终的类是系统提供的object类
    检测是否是子类:
        issubclass(class1,class2)---检测class1是否为class2的子类
"""


class human:
    name = "human"

    def eat(self):
        print("乖乖吃饭~")


class father(human):
    name = "father"

    def eat(self):
        print(f"self in father = {self}")
        super().eat()  # 这里会调用第一个父类的方法
        print(f"super().name = {super().name}")
        print("大口吃饭~")


class mother(human):
    name = "mother"

    def eat(self):
        super().eat()  # 这里会调用第一个父类的方法
        print(f"super().name = {super().name}")
        print("小口吃饭~")


class child(father,mother):
    name = "child"

    def eat(self):
        print(f"self in child = {self}")
        super().eat()  # 这里会调用第一个父类的方法
        print(f"super().name = { super().name}")
        print("又哭又闹吃饭饭~")


Boly = child()
print(f"child.mro() = {child.mro()}")
Boly.eat()
res = issubclass(child,mother)  # 检测child是否为mother的子类
print(res)

运行结果:

E:\Programs_Way\Python\python.exe D:/Prj/_PythonSelf/Object_oriented_programming/_8diamond_inheritance.py
child.mro() = [<class '__main__.child'>, <class '__main__.father'>, <class '__main__.mother'>, <class '__main__.human'>, <class 'object'>]
self in child = <__main__.child object at 0x0000019347F3B940>
self in father = <__main__.child object at 0x0000019347F3B940>
乖乖吃饭~
super().name = human
小口吃饭~
super().name = mother
大口吃饭~
super().name = father
又哭又闹吃饭饭~
True

Process finished with exit code 0E:\Programs_Way\Python\python.exe D:/Prj/_PythonSelf/Object_oriented_programming/_8diamond_inheritance.py
child.mro() = [<class '__main__.child'>, <class '__main__.father'>, <class '__main__.mother'>, <class '__main__.human'>, <class 'object'>]
self in child = <__main__.child object at 0x0000019347F3B940>
self in father = <__main__.child object at 0x0000019347F3B940>
乖乖吃饭~
super().name = human
小口吃饭~
super().name = mother
大口吃饭~
super().name = father
又哭又闹吃饭饭~
True

Process finished with exit code 0
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jasmine-Lily

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值