python学习笔记-继承
1. 经典类与新式类
经典类:
Class 类名:
代码
新式类:
Class 类名(object):
代码
Object:所有类的顶级类 ,括号里面可以是其他的父类
2. 继承的写法
子类默认继承父类所有的方法以及属性 Object是顶级类
class A(object):
def __init__(self):
self.num = 1
def info_print(self):
print(self.num)
class B(A):
pass
result = B() #创建B的对象
result.info_print() #由于继承关系 所以B的对象可以调用A的方法
3. 单继承
class Master(object):
def __init__(self):
self.kongfu = '[煎饼果子配方]'
def make_cake(self):
print(f'运用{self.kongfu}制作煎饼果子')
class Prentice(Master):
pass
tudi = Prentice()
print(tudi.kongfu) #打印属性
tudi.make_cake()
4. 多继承
多继承:继承多个父类 默认使用第一个父类的属性以及方法
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):
pass
daqiu = Prentice()
print(daqiu.kongfu)
daqiu.make_cake()
5. 重写
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):
print(f'运用{self.kongfu}制作煎饼果子')
daqiu = Prentice()
print(daqiu.kongfu)
daqiu.make_cake()
print(Prentice.__mro__) #快速查看继承关系
6. 子类调用父类同名的方法和属性
Prentice重写了父类的属性以及方法, 子类的属性与方法和父类的属性以及方法都是相同的,所以当我调用make_cake方法时,肯定调用的是子类的方法,所以为了能够调用父类的方法,在子类中定义两个方法,分别为:make_master_cake以及make_school_cake方法,这两个方法用来调用父类的方法,但是为了保证调用的是父类的属性,必须在调用前进行父类的初始化,那么如果我要调用子类的Make_cake方法(事先已经调用了make_master_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(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()
print(daqiu.kongfu)
daqiu.make_cake()
print(Prentice.__mro__)#快速查看继承关系
daqiu.make_master_cake()
7. 多层继承
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()
print(daqiu.kongfu)
daqiu.make_cake()
print(Prentice.__mro__)#快速查看继承关系
daqiu.make_master_cake()
class tusun(Prentice):
pass
s = tusun()
s.make_cake()
s.make_master_cake()
s.make_school_cake()
8. super使用方法
- Super(当前类名,self).函数()
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}制作煎饼果子')
super(School,self).__init__()
super(School,self).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)
#这里需要用一次 并且他的父类 school也要用一次 make_cake
def make_old_cake(self):
super(Prentice,self).__init__()
super(Prentice,self).make_cake()
s = Prentice()
s.make_old_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}制作煎饼果子')
# super(School,self).__init__()
# super(School,self).make_cake()
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)
#这里需要用一次 并且他的父类 school也要用一次 make_cake
def make_old_cake(self):
# super(Prentice,self).__init__()
# super(Prentice,self).make_cake()
super().__init__()
super().make_cake()
s = Prentice()
s.make_old_cake()
9. 私有属性
方法:在属性名和方法名前面添加两个下划线__ 保证该属性不会被子类进行直接调用
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}制作煎饼果子')
# super(School,self).__init__()
# super(School,self).make_cake()
super().__init__()
super().make_cake()
# 多继承 重写父类的重名方法 以及属性
class Prentice(School):
def __init__(self):
self.kongfu = '[独创煎饼果子配方]'
self.__money = 200000 #私有属性
# 如果是先调用了父类的属性和方法 父类属性会覆盖子类属性 故在调用前 先调用自己子类的初始化
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)
#这里需要用一次 并且他的父类 school也要用一次 make_cake
def make_old_cake(self):
# super(Prentice,self).__init__()
# super(Prentice,self).make_cake()
super().__init__()
super().make_cake()
class Q(Prentice):
pass
s = Prentice()
s.make_old_cake()
q = Q()
print(q.money) #无法继承私有属性
修改私有属性: get set 方法
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}制作煎饼果子')
# super(School,self).__init__()
# super(School,self).make_cake()
super().__init__()
super().make_cake()
# 多继承 重写父类的重名方法 以及属性
class Prentice(School):
def __init__(self):
self.kongfu = '[独创煎饼果子配方]'
self.__money = 200000 #私有属性
#获取私有属性和方法
def get_money(self):
return self.__money
def set_money(self):
self.__money = 5000
# 如果是先调用了父类的属性和方法 父类属性会覆盖子类属性 故在调用前 先调用自己子类的初始化
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)
#这里需要用一次 并且他的父类 school也要用一次 make_cake
def make_old_cake(self):
# super(Prentice,self).__init__()
# super(Prentice,self).make_cake()
super().__init__()
super().make_cake()
class Q(Prentice):
pass
s = Prentice()
s.make_old_cake()
q = Q()
print(q.get_money()) # 获取父类私有属性值