继承
- 继承
- 继承的语法
- 重写
- 扩展
- 引用
- 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