继承
子类默认继承父类的所有属性和方法
子类可以重写父类的属性和方法
注:在python中,所有类默认继承object类,object类是顶级类或基类,其他子类叫做派生类。
单继承:继承一个父类
1.多继承:一个类同时继承多个父类(当一个类有多个父类时,默认使用第一个父类的同名属性和方法)
class Master(object):
def __init__(self):
self.gongfu = '[师傅的蛋糕配方]'
def make_cake(self):
print(f'运用{self.gongfu}制作蛋糕')
class School(object):
def __init__(self):
self.gongfu = '[学校的蛋糕配方]'
def make_cake(self):
print(f'运用{self.gongfu}制作蛋糕')
#独创配方,继承学校和师傅的配方
class Prentice(School,Master):
def __init__(self):
self.gongfu = '[独创蛋糕配方]'
def make_cake(self):
print(f'运用{self.gongfu}制作蛋糕')
xiaoxiao = Prentice()
print(xiaoxiao.gongfu) #独创的蛋糕配方
xiaoxiao.make_cake()
print(Prentice.__mro__) #(<class '__main__.Prentice'>, <class '__main__.School'>, <class '__main__.Master'>, <class 'object'>)
使用mro查看继承关系
2.子类调用父类的同名方法和属性
如果也想要使用师傅的或者学校的蛋糕配方呢?
class Master(object):
def __init__(self):
self.gongfu = '[师傅的蛋糕配方]'
def make_cake(self):
print(f'运用{self.gongfu}制作蛋糕')
class School(object):
def __init__(self):
self.gongfu = '[学校的蛋糕配方]'
def make_cake(self):
print(f'运用{self.gongfu}制作蛋糕')
#独创配方,继承学校和师傅的配方
class Prentice(School,Master):
def __init__(self):
self.gongfu = '[独创蛋糕配方]'
def make_cake(self):
print(f'运用{self.gongfu}制作蛋糕')
#调用父类的方法,也必须调用父类的初始化(不然是上个函数的init)
def make_master_cake(self):
Master.__init__(self)
Master.make_cake(self)
def make_school_cake(self):
School.__init__(self)
School.make_cake(self)
xiaoxiao = Prentice()
print(xiaoxiao.gongfu)#运用[独创蛋糕配方]制作蛋糕
xiaoxiao.make_cake()#运用[独创蛋糕配方]制作蛋糕
xiaoxiao.make_master_cake()#运用[师傅的蛋糕配方]制作蛋糕
xiaoxiao.make_school_cake()#运用[学校的蛋糕配方]制作蛋糕
print(Prentice.__mro__) #(<class '__main__.Prentice'>, <class '__main__.School'>, <class '__main__.Master'>, <class 'object'>)
3.多层继承
继承给孙子
class Master(object):
def __init__(self):
self.gongfu = '[师傅的蛋糕配方]'
def make_cake(self):
print(f'运用{self.gongfu}制作蛋糕')
class School(object):
def __init__(self):
self.gongfu = '[学校的蛋糕配方]'
def make_cake(self):
print(f'运用{self.gongfu}制作蛋糕')
#独创配方,继承学校和师傅的配方
class Prentice(School,Master):
def __init__(self):
self.gongfu = '[独创蛋糕配方]'
def make_cake(self):
print(f'运用{self.gongfu}制作蛋糕')
#调用父类的方法,也必须调用父类的初始化(不然是上个函数的init)
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
xiaoxiao = Tusun()
print(xiaoxiao.gongfu)#运用[独创蛋糕配方]制作蛋糕
xiaoxiao.make_cake()#运用[独创蛋糕配方]制作蛋糕
xiaoxiao.make_master_cake()#运用[师傅的蛋糕配方]制作蛋糕
xiaoxiao.make_school_cake()#运用[学校的蛋糕配方]制作蛋糕
print(Prentice.__mro__) #(<class '__main__.Prentice'>, <class '__main__.School'>, <class '__main__.Master'>, <class 'object'>)
4.super()调用父类的方法
使用super()可以自动查找父类。调用顺序遵循__mro__类属性的顺序,比较适合单继承使用
class Master(object):
def __init__(self):
self.gongfu = '[师傅的蛋糕配方]'
def make_cake(self):
print(f'运用{self.gongfu}制作蛋糕')
class School(Master):
def __init__(self):
self.gongfu = '[学校的蛋糕配方]'
def make_cake(self):
print(f'运用{self.gongfu}制作蛋糕')
super().__init__()
super().make_cake()
# 独创配方,继承学校和师傅的配方
class Prentice(School):
def __init__(self):
self.gongfu = '[独创蛋糕配方]'
def make_cake(self):
print(f'运用{self.gongfu}制作蛋糕')
# 调用父类的方法,也必须调用父类的初始化(不然是上个函数的init)
def make_master_cake(self):
Master.__init__(self)
Master.make_cake(self)
def make_school_cake(self):
School.__init__(self)
School.make_cake(self)
#调用父类的方法,super不需要写方法名
def make_old_cake(self):
super().__init__()
super().make_cake()
xiaoxiao = Prentice()
xiaoxiao.make_old_cake()
#运用[学校的蛋糕配方]制作蛋糕
#运用[师傅的蛋糕配方]制作蛋糕
5.定义私有属性和方法
在python中,可以为实例属性和方法设置私有权限,即设置某个实例属性或方法不继承给子类。
class Master(object):
def __init__(self):
self.gongfu = '[师傅的蛋糕配方]'
def make_cake(self):
print(f'运用{self.gongfu}制作蛋糕')
class School(object):
def __init__(self):
self.gongfu = '[学校的蛋糕配方]'
def make_cake(self):
print(f'运用{self.gongfu}制作蛋糕')
super().__init__()
super().make_cake()
# 独创配方,继承学校和师傅的配方
class Prentice(School,Master):
def __init__(self):
self.gongfu = '[独创蛋糕配方]'
#定义私有属性
self.__money = 1000000
#定义私有方法
def __info_print(self):
print(self.gongfu)
print(self.__money)
def make_cake(self):
print(f'运用{self.gongfu}制作蛋糕')
# 调用父类的方法,也必须调用父类的初始化(不然是上个函数的init)
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
xiaoxiao = Prentice()
print(xiaoxiao.__money) #对象不能访问私有属性和私有方法
xiaoxiao.__info_print()
xiaosun = Tusun()
print(xiaosun.__money) #子类无法继承父类的私有属性和私有方法
xiaosun.__info_print()
5.1获取和修改私有属性值
在python中,一般定义函数名get_xx用来获取私有属性,定义set_xx用来修改私有属性值
class Master(object):
def __init__(self):
self.gongfu = '[师傅的蛋糕配方]'
def make_cake(self):
print(f'运用{self.gongfu}制作蛋糕')
class School(object):
def __init__(self):
self.gongfu = '[学校的蛋糕配方]'
def make_cake(self):
print(f'运用{self.gongfu}制作蛋糕')
super().__init__()
super().make_cake()
# 独创配方,继承学校和师傅的配方
class Prentice(School,Master):
def __init__(self):
self.gongfu = '[独创蛋糕配方]'
#定义私有属性
self.__money = 1000000
#获取私有属性
def get_money(self):
return self.__money
#修改私有属性
def set__money(self):
self.__money = 200000
#定义私有方法
def __info_print(self):
print(self.gongfu)
print(self.__money)
def make_cake(self):
print(f'运用{self.gongfu}制作蛋糕')
# 调用父类的方法,也必须调用父类的初始化(不然是上个函数的init)
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
xiaoxiao = Prentice()
xiaosun = Tusun()
print(xiaosun.get_money()) #获取私有属性money的值 1000000
xiaosun.set_money()#修改私有属性money的值 200000
print(xiaosun.get_money())