继承
- 继承的概念
- 单继承
- 多继承
- 子类重写父类同名的属性和方法
- 子类调用父类同名的属性和方法
- 多层继承
- super
- 私有属性和方法
一、继承的概念
生活中的继承,一般指得是子女继承父母财产
拓展:经典类或旧时类
不由任意内置类型派生出的类,称之为经典类
class 类名: 代码 .....
新式类:
class 类名(object): 代码
python面对对象的继承指的是多个类之间的所属关系,即子类默认继承父类的所由属性和方法,具体如下:
class A(object): def __init__(self): self.num=1 def info_print(self): print(self.num) #子类B class B(A): pass resoult=B() resoult.info_print() #1
在python中,所有类默认继承object类,object类是顶级类或基类;其他类成为派生类。
二、单继承
师傅把煎饼技术传授给他最得意的弟子
class Master(object): def __init__(self): self.kongfu='[古法煎饼配方]' def make_cake(self): print(f'运用{self.kongfu}制作煎饼果子') class Prentice(Master): pass #用徒弟类创建对象,调用实例属性和方法 daqiu=Prentice() print(daqiu.kongfu) daqiu.make_cake() """ [古法煎饼配方] 运用[古法煎饼配方]制作煎饼果子 """
三、多继承
所谓多继承意思就是一个类同时继承了多个父类。
class Master(object): def __init__(self): self.kongfu='[古法煎饼配方]' def make_cake(self): print(f'运用{self.kongfu}制作煎饼果子') class School(): def __init__(self): self.kongfu = '[黑马煎饼配方]' def make_cake(self): print(f'运用{self.kongfu}制作煎饼果子') class Prentice(Master,School): pass #用徒弟类创建对象,调用实例属性和方法 daqiu=Prentice() print(daqiu.kongfu) daqiu.make_cake() """ [古法煎饼配方] 运用[古法煎饼配方]制作煎饼果子 """
注意:当一个类有多个父类得时候,默认使用第一个父类得同名属性和方法
四、子类重写父类同名方法和属性
掌握了师傅和培训技术后自己钻研出一套全新得煎饼果子
class Master(object): def __init__(self): self.kongfu='[古法煎饼配方]' def make_cake(self): print(f'运用{self.kongfu}制作煎饼果子') class School(): def __init__(self): self.kongfu = '[黑马煎饼配方]' def make_cake(self): print(f'运用{self.kongfu}制作煎饼果子') class Prentice(Master,School): def __init__(self): self.kongfu= '[独创煎饼配方]' def make_cake(self): print(f'运用{self.kongfu}制作煎饼果子') #用徒弟类创建对象,调用实例属性和方法 daqiu=Prentice() print(daqiu.kongfu) daqiu.make_cake() """ [独创煎饼配方] 运用[独创煎饼配方]制作煎饼果子 """
子类和父类具有同名属性和方法,默认使用子类得同名属性和方法
使用mro查看书写继承顺序
print(Prentice.__mro__) #(<class '__main__.Prentice'>, <class '__main__.Master'>, <class '__main__.School'>, <class 'object'>)
五、子类调用父类得同名方法和属性
class Master(object): def __init__(self): self.kongfu='[古法煎饼配方]' def make_cake(self): print(f'运用{self.kongfu}制作煎饼果子') class School(): def __init__(self): self.kongfu = '[黑马煎饼配方]' def make_cake(self): print(f'运用{self.kongfu}制作煎饼果子') class Prentice(Master,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) #用徒弟类创建对象,调用实例属性和方法 daqiu=Prentice() print(daqiu.kongfu) daqiu.make_cake() daqiu.make_master_cake() daqiu.make_school_cake() daqiu.make_cake() """ [独创煎饼配方] 运用[独创煎饼配方]制作煎饼果子 运用[古法煎饼配方]制作煎饼果子 运用[黑马煎饼配方]制作煎饼果子 运用[独创煎饼配方]制作煎饼果子 """
六 多层继承
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(Master,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) 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.1super() # 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): #方法一 如果定义得类名修改,这里也要修改,麻烦,代码量大,冗余 # School.__init__(self) # School.make_cake(self) # Master.__init__(self) # Master.make_cake(self) #方法2 super() #super().函数() # super(Prentice,self).__init__() # super(Prentice,self).make_cake() super().__init__() super().make_cake() daqiu=Prentice() daqiu.make_old_cake() """ 运用[黑马煎饼配方]制作煎饼果子 运用[古法煎饼配方]制作煎饼果子 """
注意:
使用super()可以自动查找父类。调用顺序遵循——mro—类属性得顺序。比较适合单继承使用。
八私有权限
8.1定义私有属性和方法
在python中,可以为实例属性和方法设置私有权限,即设置某个实例属性或实例方法不继承给子类。
设置私有权限得方法:在属性名和方法名前面 加上两个下滑线——。
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.1super() # super(School, self).__init__() # super(School, self).make_cake() #2.2无参数 super().__init__() super().make_cake() class Prentice(School,Master): def __init__(self): self.kongfu= '[独创煎饼配方]' #设置私有属性 self.money=200000 #self.__money = 200000 def __info_prin(self): print('这是私有方法') 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): #方法一 如果定义得类名修改,这里也要修改,麻烦,代码量大,冗余 # School.__init__(self) # School.make_cake(self) # Master.__init__(self) # Master.make_cake(self) #方法2 super() #super().函数() # super(Prentice,self).__init__() # super(Prentice,self).make_cake() super().__init__() super().make_cake() class tusun(Prentice): pass xiaoqiu =tusun() print(xiaoqiu.money) xiaoqiu.__info_prin # daqiu=Prentice() # print(daqiu.money) # print(daqiu.__money) """ """
注意:私有属性和私有方法只能在类里面访问和修改
8.2获取和修改私有属性值
在python中,一般定义函数名get_xx用来获取私有属性,定义set_xx用来修改私有属性。
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.1super() # super(School, self).__init__() # super(School, self).make_cake() #2.2无参数 super().__init__() super().make_cake() class Prentice(School,Master): def __init__(self): self.kongfu= '[独创煎饼配方]' #设置私有属性 ##self.money=200000 self.__money = 200000 #修改私有属性 def set_money(self): self.__money=500 #获取私有属性 def get_money(self): return self.__money def __info_prin(self): print('这是私有方法') 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): #方法一 如果定义得类名修改,这里也要修改,麻烦,代码量大,冗余 # School.__init__(self) # School.make_cake(self) # Master.__init__(self) # Master.make_cake(self) #方法2 super() #super().函数() # super(Prentice,self).__init__() # super(Prentice,self).make_cake() super().__init__() super().make_cake() class tusun(Prentice): pass xiaoqiu =tusun() print(xiaoqiu.get_money()) xiaoqiu.set_money() print(xiaoqiu.get_money()) #xiaoqiu.__info_prin # daqiu=Prentice() # print(daqiu.money) # print(daqiu.__money) """ 200000 500 """